如何:生成可远程处理的类型
要使其他应用程序域中的对象能够使用您的类的实例,该类必须从 MarshalByRefObject 继承。以下过程描述如何创建一个简单的对象,可以通过在其他应用程序域中执行的对象来创建和调用该对象。
生成可远程处理的类型
定义一个从 MarshalByRefObject 类派生的类
Public Class CustomRemotableException Inherits RemotingException … End Class
public class CustomRemotableException : RemotingException, ISerializable { … }
就像对待非可远程处理的类型一样,实现该类的方法和属性。
Private StringValue As String = "This is the RemotableType." Public Function StringMethod() As String Return StringValue End Function 'StringMethod
private string StringValue = "This is the RemotableType."; public string StringMethod(){ return StringVale; }
将该类另存为
Filename
.language-extension(或者使用您选择的其他文件名,其中的 language-extension 是您要编译的语言),然后,在保存该文件的目录中,在命令提示符处键入以下命令:vbc /t:library Filename.vb
csc /noconfig /t:library RemotableType.cs
示例
' RemotableType.vb
Imports System
Public Class RemotableType
Inherits MarshalByRefObject
Private StringValue As String = "This is the RemotableType."
Public Function StringMethod() As String
Return StringValue
End Function 'StringMethod
End Class 'RemotableType
// RemotableType.cs
using System;
public class RemotableType : MarshalByRefObject{
private string StringValue = "This is the RemotableType.";
public string StringMethod(){
return StringVale;
}
}