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

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

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

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

用Delphi 8、VB.net以及C#混合编程unit TPerson;
用Delphi 8、VB.net以及C#混合编程interface
用Delphi 8、VB.net以及C#混合编程type
用Delphi 8、VB.net以及C#混合编程  Person 
= class
用Delphi 8、VB.net以及C#混合编程  
private
用Delphi 8、VB.net以及C#混合编程    { 
Private Declarations }
用Delphi 8、VB.net以及C#混合编程  
public
用Delphi 8、VB.net以及C#混合编程    Name : 
string;
用Delphi 8、VB.net以及C#混合编程    Age : 
integer;
用Delphi 8、VB.net以及C#混合编程    constructor Create;
用Delphi 8、VB.net以及C#混合编程  
end;
用Delphi 8、VB.net以及C#混合编程implementation
用Delphi 8、VB.net以及C#混合编程constructor Person.Create;
用Delphi 8、VB.net以及C#混合编程begin
用Delphi 8、VB.net以及C#混合编程  inherited Create;
用Delphi 8、VB.net以及C#混合编程
end;
用Delphi 8、VB.net以及C#混合编程
end.

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

用Delphi 8、VB.net以及C#混合编程Imports System
用Delphi 8、VB.net以及C#混合编程
End Class

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

用Delphi 8、VB.net以及C#混合编程using System;
用Delphi 8、VB.net以及C#混合编程
using TEmployee;
用Delphi 8、VB.net以及C#混合编程
用Delphi 8、VB.net以及C#混合编程
public class Client
}

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

相关文章:

  • 2021-08-02
  • 2022-12-23
  • 2021-08-08
  • 2021-11-25
  • 2021-09-06
  • 2021-08-12
  • 2022-01-23
猜你喜欢
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案