System.Runtime.Remoting.ObjRef Class

Assembly: Mscorlib.dll
Namespace: System.Runtime.Remoting
Summary
Stores all relevant information required to generate a proxy in order to communicate with a remote object.
C# Syntax:
[Serializable]
public class ObjRef : IObjectReference, ISerializable
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 the conceptual topic at MSDN: 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.

Example
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 RemotingConfiguration.RegisterWellKnownServiceType method.
// a custom ObjRef class that outputs its status
public class MyObjRef : ObjRef {
   
   // only instantiate via 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);
   }
}

    
See also:
System.Runtime.Remoting Namespace See also:
MSDN: objectreferences | MSDN: proxies | ISerializable | RemotingServices.Marshal | RemotingServices.Unmarshal | RealProxy

System.Runtime.Remoting.ObjRef Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the ObjRef class with default values.
ctor #2 Overloaded:
.ctor(MarshalByRefObject o, Type requestedType)

Initializes a new instance of the ObjRef class to reference a specified MarshalByRefObject of a specified Type.
Public Properties
ChannelInfo Read-write

Gets or sets the IChannelInfo for the ObjRef.
EnvoyInfo Read-write

Gets or sets the IEnvoyInfo for the ObjRef.
TypeInfo Read-write

Gets or sets the IRemotingTypeInfo for the object that the ObjRef describes.
URI Read-write

Gets or sets the URI of the specific object instance.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetObjectData Populates a specified SerializationInfo with the data needed to serialize the current ObjRef instance.
GetRealObject Returns a reference to the remote object that the ObjRef describes.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IsFromThisAppDomain Returns a Boolean value indicating whether the current ObjRef instance references an object located in the current AppDomain.
IsFromThisProcess Returns a Boolean value indicating whether the current ObjRef instance references an object located in the current process.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #3 Overloaded:
.ctor(SerializationInfo info, StreamingContext context)

Initializes a new instance of the ObjRef class from serialized data.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Runtime.Remoting.ObjRef Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the ObjRef class with default values.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public ObjRef();

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the ObjRef class to reference a specified MarshalByRefObject of a specified Type.
C# Syntax:
public ObjRef(
   MarshalByRefObject o,
   Type requestedType
);
Parameters:

o

The object that the new ObjRef instance will reference.

requestedType

The Type of the object that the new ObjRef instance will reference.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the ObjRef class from serialized data.
C# Syntax:
protected ObjRef(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

The object that holds the serialized object data.

context

The contextual information about the source or destination of the exception.

Remarks
This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see ISerializable.

Return to top


Property: ChannelInfo (read-write)
Summary
Gets or sets the IChannelInfo for the ObjRef.
C# Syntax:
public virtual IChannelInfo ChannelInfo {get; set;}
Remarks
The current property holds information contributed by active channels in the process, when the ObjRef is created (see RemotingServices.Marshal for details on marshaling). This information can be used by the channels in other processes or AppDomains to decide whether or not to create a transport sink to communicate with the object represented by the current instance.
See also:
AppDomain

Return to top


Property: EnvoyInfo (read-write)
Summary
Gets or sets the IEnvoyInfo for the ObjRef.
C# Syntax:
public virtual IEnvoyInfo EnvoyInfo {get; set;}
Remarks
The current property contains a serialized chain of message sinks that gets regenerated when a proxy is created to represent the object. This is used for Context bound object types only, and represents the groups of objects that provide runtime services like transactions.
See also:
IEnvoyInfo

Return to top


Property: TypeInfo (read-write)
Summary
Gets or sets the IRemotingTypeInfo for the object that the ObjRef describes.
C# Syntax:
public virtual IRemotingTypeInfo TypeInfo {get; set;}
Remarks
ObjRef.TypeInfo contains detailed information about the type of remote object represented by the current ObjRef. The current property contains a list of interfaces that the type implements, as well as the type hierarchy. This information might be used to refine the proxy incrementally to adjust to the client's view of the remote object's type.

Return to top


Property: URI (read-write)
Summary
Gets or sets the URI of the specific object instance.
C# Syntax:
public virtual string URI {get; set;}
Remarks
A URI uniquely identifies the specific object instance.
See also:
Uri

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~ObjRef();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetObjectData(
   SerializationInfo info,
   StreamingContext context
)
Summary
Populates a specified SerializationInfo with the data needed to serialize the current ObjRef instance.
C# Syntax:
public virtual void GetObjectData(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

The SerializationInfo to populate with data.

context

The contextual information about the source or destination of the serialization.

Exceptions
Exception Type Condition
ArgumentNullException The info parameter is null.
Implements:
ISerializable.GetObjectData
Remarks
The current method fills the provided SerializationInfo with all the data needed to recreate the ObjRef.

The current method is an implementation of ISerializable.GetObjectData.

Return to top


Method: GetRealObject(
   StreamingContext context
)
Summary
Returns a reference to the remote object that the ObjRef describes.
C# Syntax:
public virtual object GetRealObject(
   StreamingContext context
);
Parameters:

context

The context where the current object resides.

Return Value:
A reference to the remote object that the ObjRef describes.
Implements:
IObjectReference.GetRealObject
Remarks
The current method is called during the fix-up stage of deserialization.
See also:
ObjectManager

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: IsFromThisAppDomain()
Summary
Returns a Boolean value indicating whether the current ObjRef instance references an object located in the current AppDomain.
C# Syntax:
public bool IsFromThisAppDomain();
Return Value:
A Boolean value indicating whether the current ObjRef instance references an object located in the current AppDomain.

Return to top


Method: IsFromThisProcess()
Summary
Returns a Boolean value indicating whether the current ObjRef instance references an object located in the current process.
C# Syntax:
public bool IsFromThisProcess();
Return Value:
A Boolean value indicating whether the current ObjRef instance references an object located in the current process.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.