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)

適用於