技术开发 频道

重用托管代码中的Classic COM 组件

Collapse 
using System;
using MyFlyerMetadata;

public class Bird : Flyer
{

public class Airplane : Flyer
{

public override String TakeOff() {

return "Airplane::TakeOff - This is .NET taking off";

public override String Fly() {

System.Console.WriteLine(base.Fly());
return "Airplane::Fly - This is .NET in the skies";


public class FlightController
{
public static void Main(String[] args)
{
Bird falcon = new Bird();
System.Console.WriteLine("BIRD: CLEARED TO TAKE OFF");
System.Console.WriteLine(falcon.TakeOff());
System.Console.WriteLine(falcon.Fly());

Airplane skyliner = new Airplane();
System.Console.WriteLine("AIRPLANE: CLEARED TO TAKE OFF");
System.Console.WriteLine(skyliner.TakeOff());
System.Console.WriteLine(skyliner.Fly());

}/* 结束 Main */

}/* 结束FlightController */
   通过使用DOS命令行里的以下命令,你可以编译上面的程序。

csc /target:exe /out:FlightClient.exe /r:MyFlyerMetadata.dll FlightClient.cs
   运行上面的程序,就能给你提供下面的输出。

BIRD: CLEARED TO TAKE OFF 
CFlyer::TakeOff - This is COM taking off
CFlyer::Fly - This is COM in the skies
AIRPLANE: CLEARED TO TAKE OFF
Airplane::TakeOff - This is .NET taking off
CFlyer::Fly - This is COM in the skies
Airplane::Fly - This is .NET in the skies
    使用Bird和 Airplane 托管类型的使用者不需要知道这些类型是通过继承来重用现有的COM组件。如果必要的话,在COM 组件中,托管类型用它自己的执行覆盖所有的方法。这个重用模式,也就是托管代码从非托管代码继承的地方,被称作混合模式继承重用模式。
0
相关文章