用Delphi 、VB.net以及C#混合编程
转载至:http://www.cnblogs.com/zhenyulu/articles/53834.html
这个礼拜三晚上的.net讲座被取消了,原因是与我的课程冲突,并且近20天内不允许停调课。所以只能在这里将我讲座中的部分演示内容预先公布一下了。
演示内容之一便是“同一平台、多种语言”。在.net的CLR平台上不同语言编写的程序可以相互调用。其UML图如下:
我们使用Delphi 8编写Person类,并编译成DLL文件。代码如下:
 unit TPerson;
unit TPerson; interface
interface type
type Person = class
  Person = class private
  private { Private Declarations }
    { Private Declarations } public
  public Name : string;
    Name : string; Age : integer;
    Age : integer; constructor Create;
    constructor Create; end;
  end; implementation
implementation constructor Person.Create;
constructor Person.Create; begin
begin inherited Create;
  inherited Create; end;
end; end.
end.
在VB.NET添加对Delphi编写的DLL的引用,并编写继承自Person类的Employee类。
 Imports System
Imports System
 Public Class Employee
Public Class Employee Inherits TPerson.Person
    Inherits TPerson.Person
 Public Salary As Int32
    Public Salary As Int32
 Public Sub Show()
    Public Sub Show() Console.WriteLine("The Name is: " & Me.Name)
        Console.WriteLine("The Name is: " & Me.Name) Console.WriteLine("The Age is:" & Me.Age)
        Console.WriteLine("The Age is:" & Me.Age) Console.WriteLine("The Salary is:" & Me.Salary)
        Console.WriteLine("The Salary is:" & Me.Salary) End Sub
    End Sub
 End Class
End Class
下面的工作就是用C#编写代码调用Delphi与VB.NET生成的DLL。分别将两个DLL的引用添加到项目中,然后编写调用程序:
 using System;
using System; using TEmployee;
using TEmployee;
 public class Client
public class Client {
{ public static void Main()
  public static void Main() {
  { Employee e = new Employee();
    Employee e = new Employee(); e.Name = "Tom";
    e.Name = "Tom"; e.Age = 22;
    e.Age = 22; e.Salary = 1500;
    e.Salary = 1500; e.Show();
    e.Show(); }
  } }
}到此为止,程序编写完成,看看效果吧。完整的程序代码可以从这里下载。
 
                    
                     
                    
                 
                    
                
 
    
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号