本主题介绍一项传统技术,保留该技术是为了向后兼容现有的应用程序,不建议对新的开发使用该技术。现在应该使用 Windows Communication Foundation (WCF) 来开发分布式应用程序。
若要使其他应用程序域中的对象能够使用您的类的实例,您的类必须继承自 MarshalByRefObject。以下过程描述如何创建一个可以通过在其他应用程序域中执行的对象来创建和调用的基本对象。
注意: |
|---|
| 有关如何生成和运行此示例的完整说明,请参见如何:编译和运行基本远程处理应用程序。 |
生成可远程处理的类型
定义一个从 MarshalByRefObject 类派生的类。
Public Class RemotableType Inherits MarshalByRefObject … End Classpublic class RemotableType : MarshalByRefObject { … }像对不可远程处理的类型一样实现该类的方法和属性。调用
Console.WriteLine可使侦听器显示一个字符串。这可在以后用于演示如何调用远程对象。Public Function SayHello() As String Console.WriteLine("RemotableType.SayHello() was called!") Return "Hello, world" End Function 'StringMethodpublic string SayHello(){ Console.WriteLine("RemotableType.SayHello() was called!"); return "Hello, world"; }创建一个名为
remoting\type的目录,然后将该类在新目录中保存为RemotableType.cs或RemotingType.vb。打开命令提示符,定位到remoting\type目录,然后键入以下命令:vbc /t:library RemotableType.vbcsc /noconfig /t:library RemotableType.cs
示例
' RemotableType.vb
Imports System
Public Class RemotableType
Inherits MarshalByRefObject
Public Function SayHello() As String
Console.WriteLine("RemotableType.SayHello() was called!")
Return "Hello, world"
End Function
End Class
// RemotableType.cs
using System;
public class RemotableType : MarshalByRefObject
{
public string SayHello()
{
Console.WriteLine("RemotableType.SayHello() was called!");
return "Hello, world";
}
}
另请参见
任务
参考
概念
其他资源
生成日期:2010-02-13
注意: