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. 不同应用程序域中的对象通过跨应用程序域边界传输对象的副本或通过使用代理交换消息进行通信。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对象的基类,这些对象通过使用代理交换消息来跨应用程序域边界进行通信。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访问时,代理将被传递到远程应用程序。The first time an application in a remote application domain accesses a MarshalByRefObject, a proxy is passed to the remote application. 对代理的后续调用将封送回位于本地应用程序域中的对象。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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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()

检索控制此实例的生存期策略的当前生存期服务对象。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 的浅表副本。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)

适用于