共用方式為


RealProxy 類別

定義

提供 Proxy 的基底功能。

public ref class RealProxy abstract
public abstract class RealProxy
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class RealProxy
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public abstract class RealProxy
type RealProxy = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type RealProxy = class
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type RealProxy = class
Public MustInherit Class RealProxy
繼承
RealProxy
屬性

範例

// Create a custom 'RealProxy'.
public ref class MyProxy: public RealProxy
{
private:
   String^ myURIString;
   MarshalByRefObject^ myMarshalByRefObject;

public:
   MyProxy( Type^ myType )
      : RealProxy( myType )
   {
      
      // RealProxy uses the Type to generate a transparent proxy.
      myMarshalByRefObject = dynamic_cast<MarshalByRefObject^>(Activator::CreateInstance(myType));
      
      // Get 'ObjRef', for transmission serialization between application domains.
      ObjRef^ myObjRef = RemotingServices::Marshal( myMarshalByRefObject );
      
      // Get the 'URI' property of 'ObjRef' and store it.
      myURIString = myObjRef->URI;
      Console::WriteLine( "URI :{0}", myObjRef->URI );
   }

   virtual IMessage^ Invoke ( IMessage^ myIMessage ) override 
    {
      Console::WriteLine( "MyProxy.Invoke Start" );
      Console::WriteLine( "" );
      if ( dynamic_cast<IMethodCallMessage^>(myIMessage) )
            Console::WriteLine( "IMethodCallMessage" );

      if ( dynamic_cast<IMethodReturnMessage^>(myIMessage) )
            Console::WriteLine( "IMethodReturnMessage" );

      Type^ msgType = myIMessage->GetType();
      Console::WriteLine( "Message Type: {0}", msgType );
      Console::WriteLine( "Message Properties" );
      IDictionary^ myIDictionary = myIMessage->Properties;
      
      // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
      myIDictionary->default[ "__Uri" ] = myURIString;
      IDictionaryEnumerator^ myIDictionaryEnumerator = dynamic_cast<IDictionaryEnumerator^>(myIDictionary->GetEnumerator());
      while ( myIDictionaryEnumerator->MoveNext() )
      {
         Object^ myKey = myIDictionaryEnumerator->Key;
         String^ myKeyName = myKey->ToString();
         Object^ myValue = myIDictionaryEnumerator->Value;
         Console::WriteLine( "\t{0} : {1}", myKeyName, myIDictionaryEnumerator->Value );
         if ( myKeyName->Equals( "__Args" ) )
         {
            array<Object^>^myObjectArray = (array<Object^>^)myValue;
            for ( int aIndex = 0; aIndex < myObjectArray->Length; aIndex++ )
               Console::WriteLine( "\t\targ: {0} myValue: {1}", aIndex, myObjectArray[ aIndex ] );
         }

         if ( (myKeyName->Equals( "__MethodSignature" )) && (nullptr != myValue) )
         {
            array<Object^>^myObjectArray = (array<Object^>^)myValue;
            for ( int aIndex = 0; aIndex < myObjectArray->Length; aIndex++ )
               Console::WriteLine( "\t\targ: {0} myValue: {1}", aIndex, myObjectArray[ aIndex ] );
         }
      }

      IMessage^ myReturnMessage;
      myIDictionary->default[ "__Uri" ] = myURIString;
      Console::WriteLine( "__Uri {0}", myIDictionary->default[ "__Uri" ] );
      Console::WriteLine( "ChannelServices.SyncDispatchMessage" );
      myReturnMessage = ChannelServices::SyncDispatchMessage( myIMessage );
      
      // Push return value and OUT parameters back onto stack.
      IMethodReturnMessage^ myMethodReturnMessage = dynamic_cast<IMethodReturnMessage^>(myReturnMessage);
      Console::WriteLine( "IMethodReturnMessage.ReturnValue: {0}", myMethodReturnMessage->ReturnValue );
      Console::WriteLine( "MyProxy.Invoke - Finish" );
      return myReturnMessage;
   }
};
// Create a custom 'RealProxy'.
public class MyProxy : RealProxy
{
   String myURIString;
   MarshalByRefObject myMarshalByRefObject;

   public MyProxy(Type myType) : base(myType)
   {
      // RealProxy uses the Type to generate a transparent proxy.
      myMarshalByRefObject = (MarshalByRefObject)Activator.CreateInstance((myType));
      // Get 'ObjRef', for transmission serialization between application domains.
      ObjRef myObjRef = RemotingServices.Marshal(myMarshalByRefObject);
      // Get the 'URI' property of 'ObjRef' and store it.
      myURIString = myObjRef.URI;
      Console.WriteLine("URI :{0}", myObjRef.URI);
   }

   public override IMessage Invoke(IMessage myIMessage)
   {
      Console.WriteLine("MyProxy.Invoke Start");
      Console.WriteLine("");

      if (myIMessage is IMethodCallMessage)
         Console.WriteLine("IMethodCallMessage");

      if (myIMessage is IMethodReturnMessage)
         Console.WriteLine("IMethodReturnMessage");

      Type msgType = myIMessage.GetType();
      Console.WriteLine("Message Type: {0}", msgType.ToString());
      Console.WriteLine("Message Properties");
      IDictionary myIDictionary = myIMessage.Properties;
      // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
      myIDictionary["__Uri"] = myURIString;
      IDictionaryEnumerator myIDictionaryEnumerator =
         (IDictionaryEnumerator) myIDictionary.GetEnumerator();

      while (myIDictionaryEnumerator.MoveNext())
      {
         Object myKey = myIDictionaryEnumerator.Key;
         String myKeyName = myKey.ToString();
         Object myValue = myIDictionaryEnumerator.Value;

         Console.WriteLine("\t{0} : {1}", myKeyName,
            myIDictionaryEnumerator.Value);
         if (myKeyName == "__Args")
         {
            Object[] myObjectArray = (Object[])myValue;
            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex,
                  myObjectArray[aIndex]);
         }

         if ((myKeyName == "__MethodSignature") && (null != myValue))
         {
            Object[] myObjectArray = (Object[])myValue;
            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex,
                  myObjectArray[aIndex]);
         }
      }

      IMessage myReturnMessage;

      myIDictionary["__Uri"] = myURIString;
      Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);

      Console.WriteLine("ChannelServices.SyncDispatchMessage");
      myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);

      // Push return value and OUT parameters back onto stack.

      IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)
         myReturnMessage;
      Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}",
         myMethodReturnMessage.ReturnValue);

      Console.WriteLine("MyProxy.Invoke - Finish");

      return myReturnMessage;
   }
}
' Create a custom 'RealProxy'.
Public Class MyProxy
   Inherits RealProxy
   Private myURIString As String
   Private myMarshalByRefObject As MarshalByRefObject

   <PermissionSet(SecurityAction.LinkDemand)> _
   Public Sub New(ByVal myType As Type)
      MyBase.New(myType)
      ' RealProxy uses the Type to generate a transparent proxy.
      myMarshalByRefObject = CType(Activator.CreateInstance(myType), MarshalByRefObject)
      ' Get 'ObjRef', for transmission serialization between application domains.
      Dim myObjRef As ObjRef = RemotingServices.Marshal(myMarshalByRefObject)
      ' Get the 'URI' property of 'ObjRef' and store it.
      myURIString = myObjRef.URI
      Console.WriteLine("URI :{0}", myObjRef.URI)
   End Sub

<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.Infrastructure)> _
   Public Overrides Function Invoke(ByVal myIMessage As IMessage) As IMessage
      Console.WriteLine("MyProxy.Invoke Start")
      Console.WriteLine("")

      If TypeOf myIMessage Is IMethodCallMessage Then
         Console.WriteLine("IMethodCallMessage")
      End If
      If TypeOf myIMessage Is IMethodReturnMessage Then
         Console.WriteLine("IMethodReturnMessage")
      End If
      Dim msgType As Type
      msgType = CObj(myIMessage).GetType
      Console.WriteLine("Message Type: {0}", msgType.ToString())
      Console.WriteLine("Message Properties")
      Dim myIDictionary As IDictionary = myIMessage.Properties
      ' Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
      myIDictionary("__Uri") = myURIString
      Dim myIDictionaryEnumerator As IDictionaryEnumerator = CType(myIDictionary.GetEnumerator(), _
                                                                    IDictionaryEnumerator)

      While myIDictionaryEnumerator.MoveNext()
         Dim myKey As Object = myIDictionaryEnumerator.Key
         Dim myKeyName As String = myKey.ToString()
         Dim myValue As Object = myIDictionaryEnumerator.Value

         Console.WriteLine(ControlChars.Tab + "{0} : {1}", myKeyName, myIDictionaryEnumerator.Value)
         If myKeyName = "__Args" Then
            Dim myObjectArray As Object() = CType(myValue, Object())
            Dim aIndex As Integer
            For aIndex = 0 To myObjectArray.Length - 1
               Console.WriteLine(ControlChars.Tab + ControlChars.Tab + "arg: {0} myValue: {1}", _
                                                              aIndex, myObjectArray(aIndex))
             Next aIndex
         End If

         If myKeyName = "__MethodSignature" And Not Nothing Is myValue Then
            Dim myObjectArray As Object() = CType(myValue, Object())
            Dim aIndex As Integer
            For aIndex = 0 To myObjectArray.Length - 1
               Console.WriteLine(ControlChars.Tab + ControlChars.Tab + "arg: {0} myValue: {1}", _
                                                           aIndex, myObjectArray(aIndex))
            Next aIndex
         End If
      End While

        Dim myReturnMessage As IMessage

        myIDictionary("__Uri") = myURIString
        Console.WriteLine("__Uri {0}", myIDictionary("__Uri"))

        Console.WriteLine("ChannelServices.SyncDispatchMessage")
        myReturnMessage = ChannelServices.SyncDispatchMessage(CObj(myIMessage))

        ' Push return value and OUT parameters back onto stack.
        Dim myMethodReturnMessage As IMethodReturnMessage = CType(myReturnMessage, IMethodReturnMessage)
        Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}", myMethodReturnMessage.ReturnValue)

        Console.WriteLine("MyProxy.Invoke - Finish")

        Return myReturnMessage
    End Function 'Invoke
End Class

備註

類別 RealProxyabstract Proxy 必須從中衍生的基類。

在任何類型的遠端界限上使用 物件的客戶端,實際上是使用物件的透明 Proxy。 透明 Proxy 提供實際對象位於客戶端空間中的假像。 其可藉由使用遠端基礎結構將呼叫轉送至實際對象來達成此目的。

透明 Proxy 本身是由 型 RealProxy別的 Managed 運行時間類別實例所儲存。 會 RealProxy 實作從透明 Proxy 轉送作業所需的一部分功能。 請注意,Proxy 物件會繼承 Managed 物件的相關聯語意,例如垃圾收集、欄位和方法的支援,並可擴充以形成新的類別。 Proxy 具有雙重本質:它做為與遠端物件相同的類別物件, (透明 Proxy) ,而它是受控物件本身。

您可以使用 Proxy 物件,而不需考慮 內 AppDomain的任何遠端細分。

注意

這個類別會在類別層級建立連結需求和繼承需求。 SecurityException當立即呼叫端或衍生類別沒有基礎結構權限時,就會擲回 。 如需安全性需求的詳細資訊,請參閱 連結需求繼承需求

給實施者的注意事項

當您繼承自 RealProxy時,必須覆寫 Invoke(IMessage) 方法。

建構函式

RealProxy()

使用預設值,初始化 RealProxy 類別的新執行個體。

RealProxy(Type)

初始化 RealProxy 類別的新執行個體,這個執行個體代表指定 Type 的遠端物件。

RealProxy(Type, IntPtr, Object)

初始化 RealProxy 類別的新執行個體。

方法

AttachServer(MarshalByRefObject)

附加目前 Proxy 執行個體到指定的遠端 MarshalByRefObject

CreateObjRef(Type)

建立指定物件型別的 ObjRef,並向遠端基礎結構將它註冊為用戶端啟動的物件。

DetachServer()

將目前 Proxy 執行個體從它所代表的遠端伺服器物件上中斷連結。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetCOMIUnknown(Boolean)

要求目前 Proxy 執行個體所表示之物件的 Unmanaged 參考。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

RealProxy 的目前執行個體所代表物件的透明 Proxy 加入至 SerializationInfo

GetProxiedType()

傳回 Type 的目前執行個體所代表物件的 RealProxy

GetStubData(RealProxy)

擷取替指定之 Proxy 儲存的 Stub 資料。

GetTransparentProxy()

傳回 RealProxy 的目前執行個體的透明 Proxy。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetUnwrappedServer()

傳回目前 Proxy 執行個體所表示的伺服器物件。

InitializeServerObject(IConstructionCallMessage)

初始化遠端物件 (Type 的目前執行個體用指定的 RealProxy 來表示) 的物件 IConstructionCallMessage 的新執行個體。

Invoke(IMessage)

在衍生類別中覆寫時,會在目前執行個體所表示之遠端物件上叫用 (Invoke) 提供的 IMessage 中指定的方法。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SetCOMIUnknown(IntPtr)

存放目前執行個體所代表物件的 Unmanaged Proxy。

SetStubData(RealProxy, Object)

設定指定 Proxy 的 Stub 資料。

SupportsInterface(Guid)

要求具有指定 ID 的 COM 介面。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於