This documentation is archived and is not being maintained.

ObjRef Class

Stores all relevant information required to generate a proxy in order to communicate with a remote object.

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

System.Object
   System.Runtime.Remoting.ObjRef

[Visual Basic]
<Serializable>
Public Class ObjRef
   Implements IObjectReference, ISerializable
[C#]
[Serializable]
public class ObjRef : IObjectReference, ISerializable
[C++]
[Serializable]
public __gc class ObjRef : public IObjectReference, ISerializable
[JScript]
public
   Serializable
class ObjRef implements IObjectReference, ISerializable

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 ObjRef is a serializable representation of an object that extends MarshalByRefObject (MBR). An ObjRef is used to transfer an object reference across an AppDomain boundary. Creating an ObjRef for an object is known as marshaling. You can create an ObjRef (marshal a MarshalByRefObject) either explicitly, by registering the MBR object with the remoting infrastructure (see RemotingConfiguration and RemotingServices.Marshal), or implicitly, by passing an MBR object as a parameter when calling a remote object. Remoting uses ObjRef objects to store and transmit all the relevant information about the MarshalByRefObject being remoted.

The ObjRef contains information that describes the Type and class of the object being marshaled, its exact location, and communication-related information on how to reach the remoting subdivision where the object is located.

After a class implementing MarshalByRefObject is marshaled, the ObjRef that represents it is transferred through a channel into another AppDomain, possibly in another process or computer. When the ObjRef is deserialized (see XML and SOAP Serialization) in the target AppDomain, it is parsed to create a transparent proxy for the remote MBR object. This operation is known as unmarshaling.

A transparent proxy is an object that provides the illusion that the actual object resides in the client's space. It achieves this by forwarding calls made on it to the real object using the remoting infrastructure. The transparent proxy is itself housed by an instance of a managed run-time class of type RealProxy. The RealProxy implements a part of the functionality needed to forward the operations from the transparent proxy.

A proxy object can be used without regard to any remoting subdivisions within an AppDomain. Applications need not distinguish between proxy references and object references. However, service providers dealing with issues such as activation, lifetime management, and transactions need to make such distinctions.

Note   This class makes a link demand and and inheritance demand at the class level. A SecurityException is thrown when either the immediate caller or the derived class does not have infrastructure permission. For details about security demands, see Link Demands and Inheritance Demands.

Example

[Visual Basic, C#, C++] The following code example demonstrates the use of a custom ObjRef. To view the activation code that tests the custom ObjRef, see the example for the RegisterWellKnownServiceType method.

[Visual Basic] 
' a custom ObjRef class that outputs its status
Public Class MyObjRef
   Inherits ObjRef

   ' only instantiate using marshaling or deserialization
   Private Sub New()
   End Sub

   Public Sub New(ByVal o As MarshalByRefObject, ByVal t As Type)
      MyBase.New(o, t)
      Console.WriteLine("Created MyObjRef.")
      ORDump()
   End Sub

   Public Sub New(ByVal i As SerializationInfo, ByVal c As StreamingContext)
      MyBase.New(i, c)
      Console.WriteLine("Deserialized MyObjRef.")
   End Sub

   Public Overrides Sub GetObjectData(ByVal s As SerializationInfo, ByVal c As StreamingContext)
      ' After calling the base method, change the type from ObjRef to MyObjRef
      MyBase.GetObjectData(s, c)
      s.SetType([GetType]())
      Console.WriteLine("Serialized MyObjRef.")
   End Sub

   Public Overrides Function GetRealObject(ByVal context As StreamingContext) As [Object]
      If IsFromThisAppDomain() Or IsFromThisProcess() Then
         Console.WriteLine("Returning actual object referenced by MyObjRef.")
         Return MyBase.GetRealObject(context)
      Else
         Console.WriteLine("Returning proxy to remote object.")
         Return RemotingServices.Unmarshal(Me)
      End If
   End Function

   Public Sub ORDump()
      Console.WriteLine(" --- Reporting MyObjRef Info --- ")
      Console.WriteLine("Reference to {0}.", TypeInfo.TypeName)
      Console.WriteLine("URI is {0}.", URI)

      Console.WriteLine(ControlChars.Cr + "Writing EnvoyInfo: ")
      If Not (EnvoyInfo Is Nothing) Then
         Dim EISinks As IMessageSink = EnvoyInfo.EnvoySinks
         Dim count As Integer = 0
         While Not (EISinks Is Nothing)
            Console.WriteLine(ControlChars.Tab + "Interated through sink #{0}", (count = count + 1))
            EISinks = EISinks.NextSink
         End While
      Else
         Console.WriteLine(ControlChars.Tab + " {no sinks}")
      End If
      Console.WriteLine(ControlChars.Cr + "Writing ChannelInfo: ")
      Dim i As Integer
      For i = 0 To ChannelInfo.ChannelData.Length - 1
         Console.WriteLine(ControlChars.Tab + "Channel: {0}", ChannelInfo.ChannelData(i))
      Next i
      Console.WriteLine(" ----------------------------- ")
   End Sub
   
End Class


' a class that uses MyObjRef
Public Class LocalObject
   Inherits MarshalByRefObject

   ' overriding CreateObjRef will allow us to return a custom ObjRef
   Public Overrides Function CreateObjRef(ByVal t As Type) As ObjRef
      Return New MyObjRef(Me, t)
   End Function

End Class

[C#] 
// a custom ObjRef class that outputs its status
public class MyObjRef : ObjRef {
   
   // only instantiate using marshaling or deserialization
   private MyObjRef() { }

   public MyObjRef(MarshalByRefObject o, Type t) : base(o, t)  {
      Console.WriteLine("Created MyObjRef.");
      ORDump();
   }

   public MyObjRef(SerializationInfo i, StreamingContext c) : base(i, c) {
      Console.WriteLine("Deserialized MyObjRef.");
   }

   public override void GetObjectData(SerializationInfo s, StreamingContext c) {
      // After calling the base method, change the type from ObjRef to MyObjRef
      base.GetObjectData(s, c);
      s.SetType(GetType());
      Console.WriteLine("Serialized MyObjRef.");
   }

   public override Object GetRealObject(StreamingContext context) {

      if ( IsFromThisAppDomain() || IsFromThisProcess() ) {
         Console.WriteLine("Returning actual object referenced by MyObjRef.");
         return base.GetRealObject(context);
      }
      else {
         Console.WriteLine("Returning proxy to remote object.");
         return RemotingServices.Unmarshal(this);
      }
   }   

   public void ORDump() {

      Console.WriteLine(" --- Reporting MyObjRef Info --- ");
      Console.WriteLine("Reference to {0}.", TypeInfo.TypeName);
      Console.WriteLine("URI is {0}.", URI);
      Console.WriteLine("\nWriting EnvoyInfo: ");

      if ( EnvoyInfo != null) {
         
         IMessageSink EISinks = EnvoyInfo.EnvoySinks;
         while (EISinks != null) {
            Console.WriteLine("\tSink: " + EISinks.ToString());  
            EISinks = EISinks.NextSink;
         }
      }
      else
         Console.WriteLine("\t {no sinks}");

      Console.WriteLine("\nWriting ChannelInfo: ");
      for (int i = 0; i < ChannelInfo.ChannelData.Length; i++)
         Console.WriteLine ("\tChannel: {0}", ChannelInfo.ChannelData[i]);
      Console.WriteLine(" ----------------------------- ");
   }
}


// a class that uses MyObjRef
public class LocalObject : MarshalByRefObject {
   // overriding CreateObjRef will allow us to return a custom ObjRef
   public override ObjRef CreateObjRef(Type t) {
      return new MyObjRef(this, t);
   }
}

[C++] 
// a custom ObjRef class that outputs its status
public __gc class MyObjRef : public ObjRef 
{
    // only instantiate using marshaling or deserialization
private:
    MyObjRef() { }

public:
    MyObjRef(MarshalByRefObject* o, Type* t) : ObjRef(o, t) 
    {
        Console::WriteLine(S"Created MyObjRef.");
        ORDump();
    }

public:
    MyObjRef(SerializationInfo* i, StreamingContext c) : ObjRef(i, c) 
    {
        Console::WriteLine(S"Deserialized MyObjRef.");
    }

public:
    void GetObjectData(SerializationInfo* s, StreamingContext c) 
    {
        // After calling the base method, change the type from ObjRef to MyObjRef
        ObjRef::GetObjectData(s, c);
        s->SetType(GetType());
        Console::WriteLine(S"Serialized MyObjRef.");
    }

public:
    Object* GetRealObject(StreamingContext context) 
    {
        if (IsFromThisAppDomain() || IsFromThisProcess()) 
        {
            Console::WriteLine(S"Returning actual Object* referenced by MyObjRef.");
            return ObjRef::GetRealObject(context);
        }
        else 
        {
            Console::WriteLine(S"Returning proxy to remote Object*.");
            return RemotingServices::Unmarshal(this);
        }
    }   

public:
    void ORDump() 
    {
        Console::WriteLine(S" --- Reporting MyObjRef Info --- ");
        Console::WriteLine(S"Reference to {0}.", TypeInfo->TypeName);
        Console::WriteLine(S"URI is {0}.", URI);
        Console::WriteLine(S"\nWriting EnvoyInfo: ");

        if (EnvoyInfo != 0) 
        {

            IMessageSink* EISinks = EnvoyInfo->EnvoySinks;
            while (EISinks != 0) {

                Console::WriteLine(S"\tSink: {0}", EISinks);  
                EISinks = EISinks->NextSink;
            }
        } 
        else
            Console::WriteLine(S"\t {no sinks}");

        Console::WriteLine(S"\nWriting ChannelInfo: ");
        for (int i = 0; i < ChannelInfo->ChannelData->Length; i++)
            Console::WriteLine (S"\tChannel: {0}", ChannelInfo->ChannelData->Item[i]);
        Console::WriteLine(S" ----------------------------- ");
    }
};

// a class that uses MyObjRef
public __gc class LocalObject : public MarshalByRefObject 
{
    // overriding CreateObjRef will allow us to return a custom ObjRef
public:
    ObjRef* CreateObjRef(Type* t) 
    {
        return new MyObjRef(this, t);
    }
};

[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.Runtime.Remoting

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

Assembly: Mscorlib (in Mscorlib.dll)

.NET Framework Security: 

See Also

ObjRef Members | System.Runtime.Remoting Namespace | ISerializable | RemotingServices.Marshal | RemotingServices.Unmarshal | RealProxy

Show: