Export (0) Print
Expand All

ObjRef Constructor

.NET Framework 1.1

Initializes a new instance of the ObjRef class.

Overload List

Initializes a new instance of the ObjRef class with default values.

[Visual Basic] Public Sub New()
[C#] public ObjRef();
[C++] public: ObjRef();
[JScript] public function ObjRef();

Initializes a new instance of the ObjRef class to reference a specified MarshalByRefObject of a specified Type.

[Visual Basic] Public Sub New(MarshalByRefObject, Type)
[C#] public ObjRef(MarshalByRefObject, Type);
[C++] public: ObjRef(MarshalByRefObject*, Type*);
[JScript] public function ObjRef(MarshalByRefObject, Type);

Initializes a new instance of the ObjRef class from serialized data.

[Visual Basic] Protected Sub New(SerializationInfo, StreamingContext)
[C#] protected ObjRef(SerializationInfo, StreamingContext);
[C++] protected: ObjRef(SerializationInfo*, StreamingContext);
[JScript] protected function ObjRef(SerializationInfo, StreamingContext);

Example

[C#, C++] Note   This example shows how to use one of the overloaded versions of the ObjRef constructor. For other examples that might be available, see the individual overload topics.
[C#] 
// 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);
   }
}

[C++] 
// a custom ObjRef class that outputs its status
public __gc class MyObjRef : public ObjRef 
{
    // only instantiate via 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);
    }
};

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

See Also

ObjRef Class | ObjRef Members | System.Runtime.Remoting Namespace

Show:
© 2014 Microsoft