共用方式為


HOW TO:建置可遠端處理的型別

本主題專門說明一項為了在現有應用程式中提供回溯相容性而保留的舊有技術,不建議用於新的開發工作。分散式應用程式應使用 Windows Communication Foundation (WCF) 進行開發。

若要啟用其他應用程式定義域中的物件來使用您的類別執行個體,您的類別必須繼承自 MarshalByRefObject。下列程序說明如何建立可以透過其他應用程式定義域中執行的物件加以建立並叫用的基本物件。

txct33xt.note(zh-tw,VS.100).gif注意:
如需如何建置並執行這個範例的完整指示,請參閱 HOW TO:編譯並執行基本遠端應用程式

若要建置可遠端處理的型別

  1. 定義衍生自 MarshalByRefObject 類別的類別。

       Public Class RemotableType
          Inherits MarshalByRefObject
        …
    End Class
    
    public class RemotableType : MarshalByRefObject
    {
        …
    }
    
  2. 如同實作不可遠端處理型別之方法和屬性的方式,實作該類別的方法和屬性。呼叫 Console.WriteLine,讓接聽程式顯示字串。之後可用來示範呼叫遠端物件。

    Public Function SayHello() As String
       Console.WriteLine("RemotableType.SayHello() was called!")
       Return "Hello, world"
    End Function 'StringMethod
    
    public string SayHello(){
       Console.WriteLine("RemotableType.SayHello() was called!");
       return "Hello, world";
    }
    
  3. 建立名為 remoting\type 的目錄,並且在新目錄中將類別儲存為 RemotableType.csRemotingType.vb。開啟命令提示字元並巡覽至 remoting\type 目錄,然後輸入下列命令:

    vbc /t:library RemotableType.vb
    
    csc /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";
    }
}

另請參閱

工作

HOW TO:建置裝載應用程式
HOW TO:建置用戶端應用程式

參考

遠端設定結構描述

概念

遠端應用程式的組態
伺服器啟動過程

其他資源

建置基本 .NET Framework 遠端處理應用程式

建置日期:2010-02-13