Export (0) Print
Expand All

ObjRef Class

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

System.Object
  System.Runtime.Remoting.ObjRef

Namespace:  System.Runtime.Remoting
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public class ObjRef : IObjectReference, ISerializable

The ObjRef type exposes the following members.

  NameDescription
Public methodObjRef()Initializes a new instance of the ObjRef class with default values.
Public methodObjRef(MarshalByRefObject, Type)Initializes a new instance of the ObjRef class to reference a specified MarshalByRefObject of a specified Type.
Protected methodObjRef(SerializationInfo, StreamingContext)Initializes a new instance of the ObjRef class from serialized data.
Top

  NameDescription
Public propertyChannelInfoGets or sets the IChannelInfo for the ObjRef.
Public propertyEnvoyInfoGets or sets the IEnvoyInfo for the ObjRef.
Public propertyTypeInfoGets or sets the IRemotingTypeInfo for the object that the ObjRef describes.
Public propertyURIGets or sets the URI of the specific object instance.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetObjectDataPopulates a specified SerializationInfo with the data needed to serialize the current ObjRef instance.
Public methodGetRealObjectReturns a reference to the remote object that the ObjRef describes.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsFromThisAppDomainReturns a Boolean value that indicates whether the current ObjRef instance references an object located in the current AppDomain.
Public methodIsFromThisProcessReturns a Boolean value that indicates whether the current ObjRef instance references an object located in the current process.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

A ObjRef is a serializable representation of an object that extends MarshalByRefObject (MBR). A ObjRef is used to transfer an object reference across a AppDomain boundary. Creating a ObjRef for an object is known as marshaling. You can create a 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 application domain, possibly in another process or computer. When the ObjRef is deserialized (see XML and SOAP Serialization) in the target application domain, 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 a 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.

This class makes a link demand and an 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.

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.

// a custom ObjRef class that outputs its status
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
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
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
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);
   }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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