ObjRef ObjRef ObjRef ObjRef Class

定義

儲存產生 Proxy 所需的所有相關資訊,以便與遠端物件通訊。Stores all relevant information required to generate a proxy in order to communicate with a remote object.

public ref class ObjRef : System::Runtime::Serialization::IObjectReference, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
[System.Serializable]
public class ObjRef : System.Runtime.Serialization.IObjectReference, System.Runtime.Serialization.ISerializable
type ObjRef = class
    interface IObjectReference
    interface ISerializable
Public Class ObjRef
Implements IObjectReference, ISerializable
繼承
ObjRefObjRefObjRefObjRef
屬性
實作

範例

下列程式碼範例示範如何使用自訂ObjRefThe following code example demonstrates the use of a custom ObjRef. 若要查看測試自訂ObjRef的啟用程式碼, 請參閱RegisterWellKnownServiceType方法的範例。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
[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
[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);
   }
}
' 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) 之物件的可序列化標記法。A ObjRef is a serializable representation of an object that extends MarshalByRefObject (MBR). 是用來AppDomain在界限之間傳輸物件參考。 ObjRefA ObjRef is used to transfer an object reference across a AppDomain boundary. ObjRef為物件建立會稱為封送處理。Creating a ObjRef for an object is known as marshaling. 您可以明確地ObjRef建立 (封MarshalByRefObject送處理 a), 方法是向遠端基礎結構註冊 mbr 物件 ( RemotingConfigurationRemotingServices.Marshal參閱和), 或在呼叫遠端時, 以隱含方式傳遞 mbr 物件做為參數。目標.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. 遠端處理ObjRef會使用物件來儲存和傳輸有關正在進行遠端MarshalByRefObject的所有相關資訊。Remoting uses ObjRef objects to store and transmit all the relevant information about the MarshalByRefObject being remoted.

包含的資訊Type描述要封送處理之物件的和類別、它的確切位置, 以及有關如何連接到物件所在之遠端關係的通訊相關資訊。 ObjRefThe 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.

封送處理類別MarshalByRefObject之後, 表示它ObjRef的會透過通道傳送到另一個應用程式域 (可能是在另一個進程或電腦中)。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. 當還原ObjRef序列化目標應用程式域中的 (請參閱XML 和 SOAP 序列化) 時, 它會經過剖析, 以建立遠端 MBR 物件的透明 proxy。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.

透明 proxy 是一種物件, 提供實際物件位於用戶端空間中的假像。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. 透明 proxy 本身是由型RealProxy別之 managed 執行時間類別的實例所裝載。The transparent proxy is itself housed by an instance of a managed run-time class of type RealProxy. RealProxy會執行從透明 proxy 轉送作業所需的部分功能。The RealProxy implements a part of the functionality needed to forward the operations from the transparent proxy.

在中, 您可以使用 proxy 物件, 而不需要考慮中AppDomain的任何遠端子分割。A proxy object can be used without regard to any remoting subdivisions within a AppDomain. 應用程式不需要區分 proxy 參考和物件參考。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. 當立即呼叫端或衍生類別沒有基礎結構許可權時,就會擲回。SecurityExceptionA 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.

建構函式

ObjRef() ObjRef() ObjRef() ObjRef()

使用預設值,初始化 ObjRef 類別的新執行個體。Initializes a new instance of the ObjRef class with default values.

ObjRef(MarshalByRefObject, Type) ObjRef(MarshalByRefObject, Type) ObjRef(MarshalByRefObject, Type) ObjRef(MarshalByRefObject, Type)

初始化 ObjRef 類別的新執行個體,以參考所指定 MarshalByRefObject 的指定 TypeInitializes a new instance of the ObjRef class to reference a specified MarshalByRefObject of a specified Type.

ObjRef(SerializationInfo, StreamingContext) ObjRef(SerializationInfo, StreamingContext) ObjRef(SerializationInfo, StreamingContext) ObjRef(SerializationInfo, StreamingContext)

從序列化資料中,初始化 ObjRef 類別的新執行個體。Initializes a new instance of the ObjRef class from serialized data.

屬性

ChannelInfo ChannelInfo ChannelInfo ChannelInfo

取得或設定 IChannelInfoObjRefGets or sets the IChannelInfo for the ObjRef.

EnvoyInfo EnvoyInfo EnvoyInfo EnvoyInfo

取得或設定 IEnvoyInfoObjRefGets or sets the IEnvoyInfo for the ObjRef.

TypeInfo TypeInfo TypeInfo TypeInfo

取得或設定 IRemotingTypeInfo 所描述之物件的 ObjRefGets or sets the IRemotingTypeInfo for the object that the ObjRef describes.

URI URI URI URI

取得或設定特定物件執行個體的 URI。Gets or sets the URI of the specific object instance.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

以序列化目前 SerializationInfo 執行個體所需的資料,填入 (Populate) 指定的 ObjRefPopulates a specified SerializationInfo with the data needed to serialize the current ObjRef instance.

GetRealObject(StreamingContext) GetRealObject(StreamingContext) GetRealObject(StreamingContext) GetRealObject(StreamingContext)

傳回 ObjRef 所描述之遠端物件的參考。Returns a reference to the remote object that the ObjRef describes.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsFromThisAppDomain() IsFromThisAppDomain() IsFromThisAppDomain() IsFromThisAppDomain()

傳回布林 (Boolean) 值,指出目前的 ObjRef 執行個體是否參考位於目前 AppDomain 中的物件。Returns a Boolean value that indicates whether the current ObjRef instance references an object located in the current AppDomain.

IsFromThisProcess() IsFromThisProcess() IsFromThisProcess() IsFromThisProcess()

傳回布林值,指出目前的 ObjRef 執行個體是否參考位於目前處理序中的物件。Returns a Boolean value that indicates whether the current ObjRef instance references an object located in the current process.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

安全性

SecurityPermission
用於操作基礎結構程式碼。for operating with infrastructure code. 需求值: InheritanceDemand;許可權值:InfrastructureDemand value: InheritanceDemand; Permission Value: Infrastructure

適用於

另請參閱