ObjRef 类

定义

存储生成代理以与远程对象通信所需的所有相关信息。

public ref class ObjRef : System::Runtime::Serialization::IObjectReference, System::Runtime::Serialization::ISerializable
[System.Serializable]
public class ObjRef : System.Runtime.Serialization.IObjectReference, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ObjRef : System.Runtime.Serialization.IObjectReference, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public class ObjRef : System.Runtime.Serialization.IObjectReference, System.Runtime.Serialization.ISerializable
[<System.Serializable>]
type ObjRef = class
    interface IObjectReference
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObjRef = class
    interface IObjectReference
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type ObjRef = class
    interface IObjectReference
    interface ISerializable
Public Class ObjRef
Implements IObjectReference, ISerializable
继承
ObjRef
属性
实现

示例

下面的代码示例演示如何使用自定义 ObjRef。 若要查看测试自定义 ObjRef的激活代码,请参阅 方法的示例 RegisterWellKnownServiceType

// a custom ObjRef class that outputs its status
[System::Security::Permissions::SecurityPermissionAttribute(
   System::Security::Permissions::SecurityAction::Demand, 
   Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)]
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::Demand, 
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]	
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::InheritanceDemand, 
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
public ref class MyObjRef: public ObjRef
{
private:

   // only instantiate using marshaling or deserialization
   MyObjRef(){}

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

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

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

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

   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 != nullptr )
      {
         IMessageSink^ EISinks = EnvoyInfo->EnvoySinks;
         while ( EISinks != nullptr )
         {
            Console::WriteLine( "\tSink: {0}", EISinks );
            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 ref class LocalObject: public MarshalByRefObject
{
public:

   // overriding CreateObjRef will allow us to return a custom ObjRef
   [System::Security::Permissions::SecurityPermissionAttribute
   (System::Security::Permissions::SecurityAction::LinkDemand,
   Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
   virtual ObjRef^ CreateObjRef( Type^ t ) override
   {
      return gcnew MyObjRef( this,t );
   }
};
// 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);
   }
}
' a custom ObjRef class that outputs its status
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
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
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
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

注解

ObjRef 对象的可序列化表示形式,该对象扩展 MarshalByRefObject (MBR) 。 ObjRef用于跨AppDomain边界传输对象引用。 ObjRef为对象创建 称为封送处理。 可以通过ObjRef将 MBR 对象注册到远程处理基础结构( (查看RemotingConfigurationRemotingServices.Marshal) )显式封送 (封送 MarshalByRefObject) ,也可以在调用远程对象时将 MBR 对象作为参数进行隐式传递。 远程处理使用 ObjRef 对象来存储和传输有关远程的所有 MarshalByRefObject 相关信息。

包含 ObjRef 描述 Type 被封送对象的 和 类的信息、其确切位置,以及有关如何访问对象所在远程处理细分的通信相关信息。

对实现 MarshalByRefObject 的类进行封送处理后, ObjRef 表示它的 将通过通道传输到另一个应用程序域(可能在另一个进程或计算机中)。 ObjRef反序列化 (在目标应用程序域中看到 XML 和 SOAP 序列化) 时,将对其进行分析,以便为远程 MBR 对象创建透明代理。 此操作称为取消封送。

透明代理是一个对象,它提供实际对象驻留在客户端空间中的幻觉。 它通过使用远程处理基础结构将对其进行的调用转发到实际对象来实现此目的。 透明代理本身由 类型的 RealProxy托管运行时类的实例托管。 实现 RealProxy 从透明代理转发操作所需的部分功能。

可以使用代理对象,而不考虑 内 AppDomain的任何远程处理细分。 应用程序不需要区分代理引用和对象引用。 但是,处理激活、生存期管理和事务等问题的服务提供商需要做出此类区分。

此类在类级别发出链接需求和继承需求。 SecurityException当直接调用方或派生类没有基础结构权限时,将引发 。 有关安全要求的详细信息,请参阅 链接需求继承需求

构造函数

ObjRef()

使用默认值初始化 ObjRef 类的新实例。

ObjRef(MarshalByRefObject, Type)

初始化 ObjRef 类的新实例以引用指定 MarshalByRefObject 的指定 Type

ObjRef(SerializationInfo, StreamingContext)

用序列化数据初始化 ObjRef 类的新实例。

属性

ChannelInfo

获取或设置 IChannelInfoObjRef

EnvoyInfo

获取或设置 IEnvoyInfoObjRef

TypeInfo

获取或设置 IRemotingTypeInfo 描述的对象的 ObjRef

URI

获取或设置特定对象实例的 URI。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

用序列化当前 SerializationInfo 实例所需的数据来填充指定的 ObjRef

GetRealObject(StreamingContext)

返回对 ObjRef 描述的远程对象的引用。

GetType()

获取当前实例的 Type

(继承自 Object)
IsFromThisAppDomain()

返回一个布尔值,该值指示当前的 ObjRef 实例是否引用位于当前 AppDomain 中的对象。

IsFromThisProcess()

返回一个布尔值,该值指示当前的 ObjRef 实例是否引用位于当前进程中的对象。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅