Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MarshalByRefObject Class

Enables access to objects across application domain boundaries in applications that support remoting.

For a list of all members of this type, see MarshalByRefObject Members.

System.Object
   System.MarshalByRefObject
      Derived classes

[Visual Basic]
<Serializable>
MustInherit Public Class MarshalByRefObject
[C#]
[Serializable]
public abstract class MarshalByRefObject
[C++]
[Serializable]
public __gc __abstract class MarshalByRefObject
[JScript]
public
   Serializable
abstract class MarshalByRefObject

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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 is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. 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 objects are accessed directly within the boundaries of the local application domain. 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.

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.

Example

[Visual Basic, C#, C++] The following sample demonstrates a class derived from MarshalByRefObject that is used later in remoting.

[Visual Basic] 
Imports System
Imports System.Runtime.Remoting

Public Class SetObjectUriForMarshalTest
    
    Class TestClass
        Inherits MarshalByRefObject
    End Class

    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

[C#] 
using System;
using System.Runtime.Remoting;

public class SetObjectUriForMarshalTest  {

    class TestClass : MarshalByRefObject {
    }

    public static void Main()  {

        TestClass obj = new TestClass();    

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

        Console.WriteLine(RemotingServices.GetObjectUri(obj));
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::Remoting;

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

    public:
        static void Main() 
        {
            TestClass * obj = new TestClass();    

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

            Console::WriteLine(RemotingServices::GetObjectUri(obj));
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

MarshalByRefObject Members | System Namespace

Show:
© 2014 Microsoft