用Delphi 、VB.net以及C#混合编程

转载至:http://www.cnblogs.com/zhenyulu/articles/53834.html

这个礼拜三晚上的.net讲座被取消了,原因是与我的课程冲突,并且近20天内不允许停调课。所以只能在这里将我讲座中的部分演示内容预先公布一下了。

演示内容之一便是“同一平台、多种语言”。在.net的CLR平台上不同语言编写的程序可以相互调用。其UML图如下:



我们使用Delphi 8编写Person类,并编译成DLL文件。代码如下:

unit TPerson;
interface
type
  Person 
= class
  
private
    { 
Private Declarations }
  
public
    Name : 
string;
    Age : 
integer;
    constructor Create;
  
end;
implementation
constructor Person.Create;
begin
  inherited Create;
end;
end.


在VB.NET添加对Delphi编写的DLL的引用,并编写继承自Person类的Employee类。

Imports System

Public Class Employee
    
Inherits TPerson.Person

    
Public Salary As Int32

    
Public Sub Show()
        Console.
WriteLine("The Name is: " & Me.Name)
        Console.
WriteLine("The Age is:" & Me.Age)
        Console.
WriteLine("The Salary is:" & Me.Salary)
    
End Sub


End Class


下面的工作就是用C#编写代码调用Delphi与VB.NET生成的DLL。分别将两个DLL的引用添加到项目中,然后编写调用程序:

using System;
using TEmployee;

public class Client
{
  
public static void Main()
  
{
    Employee e 
= new Employee();
    e.Name 
= "Tom";
    e.Age 
= 22;
    e.Salary 
= 1500;
    e.Show();
  }

}

到此为止,程序编写完成,看看效果吧。完整的程序代码可以从这里下载。

原文地址:https://www.cnblogs.com/jshchg/p/2122123.html