如何建置可遠端處理的型別
若要讓其他應用程式定義域中的物件使用您類別的執行個體,您的類別必須繼承自 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;
}
}