MarshalByRefObject 類別

定義

在支援遠端處理的應用程式中啟用跨應用程式定義域界限存取物件。Enables access to objects across application domain boundaries in applications that support remoting.

public ref class MarshalByRefObject abstract
public abstract class MarshalByRefObject
[System.Serializable]
public abstract class MarshalByRefObject
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class MarshalByRefObject
type MarshalByRefObject = class
[<System.Serializable>]
type MarshalByRefObject = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MarshalByRefObject = class
Public MustInherit Class MarshalByRefObject
繼承
MarshalByRefObject
衍生
屬性

範例

本節包含兩個程式碼範例。This section contains two code examples. 第一個程式碼範例顯示如何在另一個應用程式域中建立類別的實例。The first code example shows how to create an instance of a class in another application domain. 第二個程式碼範例顯示可用於遠端的簡單類別。The second code example shows a simple class that can be used for remoting.

範例 1Example 1

下列程式碼範例顯示在另一個應用程式域中執行程式碼的最簡單方式。The following code example shows the simplest way to execute code in another application domain. 此範例會定義名為 Worker 的類別,該類別 MarshalByRefObject 會使用顯示其執行所在之應用程式功能變數名稱稱的方法來進行繼承。The example defines a class named Worker that inherits MarshalByRefObject, with a method that displays the name of the application domain in which it is executing. 此範例會在 Worker 預設應用程式域和新的應用程式域中建立的實例。The example creates instances of Worker in the default application domain and in a new application domain.

注意

包含的元件 Worker 必須載入兩個應用程式域中,但它可以載入只存在於新的應用程式域中的其他元件。The assembly that contains Worker must be loaded into both application domains, but it could load other assemblies that would exist only in the new application domain.

using namespace System;
using namespace System::Reflection;

public ref class Worker : MarshalByRefObject
{
public:
    void PrintDomain() 
    { 
        Console::WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain::CurrentDomain->FriendlyName); 
    }
};
 
void main()
{
    // Create an ordinary instance in the current AppDomain
    Worker^ localWorker = gcnew Worker();
    localWorker->PrintDomain();
 
    // Create a new application domain, create an instance
    // of Worker in the application domain, and execute code
    // there.
    AppDomain^ ad = AppDomain::CreateDomain("New domain");
    Worker^ remoteWorker = (Worker^) ad->CreateInstanceAndUnwrap(
        Worker::typeid->Assembly->FullName,
        "Worker");
    remoteWorker->PrintDomain();
}

/* This code produces output similar to the following:

Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
 */
using System;
using System.Reflection;

public class Worker : MarshalByRefObject
{
    public void PrintDomain()
    {
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName);
    }
}

class Example
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        Worker localWorker = new Worker();
        localWorker.PrintDomain();

        // Create a new application domain, create an instance
        // of Worker in the application domain, and execute code
        // there.
        AppDomain ad = AppDomain.CreateDomain("New domain");
        Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
            typeof(Worker).Assembly.FullName,
            "Worker");
        remoteWorker.PrintDomain();
    }
}

/* This code produces output similar to the following:

Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
 */
Imports System.Reflection

Public Class Worker
    Inherits MarshalByRefObject
    
    Public Sub PrintDomain() 
        Console.WriteLine("Object is executing in AppDomain ""{0}""", _
            AppDomain.CurrentDomain.FriendlyName)
    End Sub 
End Class 

Class Example
    
    Public Shared Sub Main() 
        ' Create an ordinary instance in the current AppDomain
        Dim localWorker As New Worker()
        localWorker.PrintDomain()
        
        ' Create a new application domain, create an instance
        ' of Worker in the application domain, and execute code
        ' there.
        Dim ad As AppDomain = AppDomain.CreateDomain("New domain")
        Dim remoteWorker As Worker = CType( _
            ad.CreateInstanceAndUnwrap( _
                GetType(Worker).Assembly.FullName, _
                "Worker"), _
            Worker)
        remoteWorker.PrintDomain()
    
    End Sub 
End Class 

' This code produces output similar to the following:
'
'Object is executing in AppDomain "source.exe"
'Object is executing in AppDomain "New domain"

範例 2Example 2

下列範例將示範衍生自的類別 MarshalByRefObject ,該類別稍後會在遠端處理中使用。The following example demonstrates a class derived from MarshalByRefObject that is used later in remoting.

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Security::Permissions;

public ref class SetObjectUriForMarshalTest
{
public:
   ref class TestClass: public MarshalByRefObject{};

   [SecurityPermissionAttribute(SecurityAction::Demand, Flags=SecurityPermissionFlag::RemotingConfiguration)]   
   static void Main()
   {
      TestClass^ obj = gcnew TestClass;
      RemotingServices::SetObjectUriForMarshal( obj,  "testUri" );
      RemotingServices::Marshal(obj);
      Console::WriteLine( RemotingServices::GetObjectUri( obj ) );
   }

};
using System;
using System.Runtime.Remoting;
using System.Security.Permissions;

public class SetObjectUriForMarshalTest  {

    class TestClass : MarshalByRefObject {
    }

    [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.RemotingConfiguration)]
    public static void Main()  {

        TestClass obj = new TestClass();

        RemotingServices.SetObjectUriForMarshal(obj, "testUri");
        RemotingServices.Marshal(obj);

        Console.WriteLine(RemotingServices.GetObjectUri(obj));
    }
}
Imports System.Runtime.Remoting
Imports System.Security.Permissions


Public Class SetObjectUriForMarshalTest
    
    Class TestClass
        Inherits MarshalByRefObject
    End Class

    <SecurityPermission(SecurityAction.Demand, Flags:= SecurityPermissionFlag.RemotingConfiguration )> _
    Public Shared Sub Main()
        Dim obj As TestClass = New TestClass()

        RemotingServices.SetObjectUriForMarshal(obj, "testUri")
        RemotingServices.Marshal(obj)

        Console.WriteLine(RemotingServices.GetObjectUri(obj))
    End Sub

End Class

備註

應用程式域是作業系統進程中的一或多個應用程式所在的磁碟分割。An application domain is a partition in an operating system process where one or more applications reside. 相同應用程式域中的物件會直接進行通訊。Objects in the same application domain communicate directly. 不同應用程式域中的物件會藉由在應用程式域界限之間傳輸物件的複本,或使用 proxy 來交換訊息來進行通訊。Objects in different application domains communicate either by transporting copies of objects across application domain boundaries, or by using a proxy to exchange messages.

MarshalByRefObject 是物件的基類,可透過使用 proxy 交換訊息來跨應用程式域界限進行通訊。MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. 不是繼承自的物件 MarshalByRefObject 會以傳值方式隱含地封送處理。Objects that do not inherit from MarshalByRefObject are implicitly marshal by value. 當遠端應用程式以傳值物件參考封送處理時,會跨應用程式域界限傳遞物件的複本。When a remote application references a marshal by value object, a copy of the object is passed across application domain boundaries.

MarshalByRefObject 您可以直接在本機應用程式域的界限記憶體取物件。MarshalByRefObject objects are accessed directly within the boundaries of the local application domain. 遠端應用程式域中的應用程式第一次存取時 MarshalByRefObject ,會將 proxy 傳遞給遠端應用程式。The first time an application in a remote application domain accesses a MarshalByRefObject, a proxy is passed to the remote application. 後續對 proxy 的呼叫會封送處理回位於本機應用程式域中的物件。Subsequent calls on the proxy are marshaled back to the object residing in the local application domain.

型別必須在 MarshalByRefObject 跨應用程式域界限使用類型時繼承,而且必須複製物件的狀態,因為物件的成員無法在其建立所在的應用程式域外部使用。Types must inherit from MarshalByRefObject when the type is used across application domain boundaries, and the state of the object must not be copied because the members of the object are not usable outside the application domain where they were created.

當您從衍生物件 MarshalByRefObject 以用於跨應用程式域界限時,您不應該覆寫其任何成員,也不應該直接呼叫其方法。When you derive an object from MarshalByRefObject for use across application domain boundaries, you should not override any of its members, nor should you call its methods directly. 執行時間會辨識出衍生自的類別 MarshalByRefObject 應該跨應用程式域界限進行封送處理。The runtime recognizes that classes derived from MarshalByRefObject should be marshaled across app domain boundaries.

建構函式

MarshalByRefObject()

初始化 MarshalByRefObject 類別的新執行個體。Initializes a new instance of the MarshalByRefObject class.

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()
已過時。

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於