MarshalByRefObject MarshalByRefObject MarshalByRefObject MarshalByRefObject Class

定義

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

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

範例

本節包含兩個程式碼範例。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() MarshalByRefObject() MarshalByRefObject()

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

方法

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) 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) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

適用於