First we try, then we trust

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
这个礼拜三晚上的.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();
  }

}

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

posted on 2004-10-18 18:05  吕震宇  阅读(5054)  评论(7编辑  收藏  举报