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

備註

AObjRef是擴充物件的序列化表示法MarshalByRefObject(MBR)。A ObjRef is a serializable representation of an object that extends MarshalByRefObject (MBR). AObjRef用來傳送在物件參考AppDomain界限。A 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) 是明確地註冊 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.

ObjRef包含說明資訊Type和物件的類別所封送處理,其確切的位置和如何連線到遠端處理可再細分的物件位於其中的通訊相關的資訊。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.

在類別實作之後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 物件的 transparent 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.

Transparent 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 是RealProxyThe 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 物件可用而不考慮內任何遠端細分AppDomainA 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. ASecurityException立即呼叫端或衍生的類別沒有基礎結構權限時,會擲回。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.

建構函式

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

適用於

另請參閱