Share via


RealProxy Classe

Définition

Fournit des fonctionnalités de base aux proxies.

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
Héritage
RealProxy
Attributs

Exemples

// 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

Remarques

La RealProxy classe est la abstract classe de base à partir de laquelle les proxys doivent dériver.

Un client qui utilise un objet sur n’importe quel type de limite de communication à distance utilise en fait un proxy transparent pour l’objet. Le proxy transparent fournit l’illusion que l’objet réel réside dans l’espace du client. Pour ce faire, il transfère les appels effectués sur l’objet réel à l’aide de l’infrastructure de communication à distance.

Le proxy transparent est lui-même hébergé par une instance d’une classe runtime managée de type RealProxy. Il RealProxy implémente une partie de la fonctionnalité nécessaire pour transférer les opérations à partir du proxy transparent. Notez qu’un objet proxy hérite de la sémantique associée d’objets managés tels que le garbage collection, la prise en charge des champs et des méthodes, et peut être étendu pour former de nouvelles classes. Le proxy a une double nature : il agit comme un objet de la même classe que l’objet distant (proxy transparent), et il s’agit d’un objet managé lui-même.

Un objet proxy peut être utilisé sans tenir compte des sous-divisions de communication à distance au sein d’un AppDomain.

Notes

Cette classe effectue une demande de liaison et une demande d’héritage au niveau de la classe. A SecurityException est levée lorsque l’appelant immédiat ou la classe dérivée n’a pas d’autorisation d’infrastructure. Pour plus d’informations sur les demandes de sécurité, consultez Les demandes de liaison et les demandes d’héritage.

Notes pour les responsables de l’implémentation

Lorsque vous héritez de RealProxy, vous devez remplacer la Invoke(IMessage) méthode.

Constructeurs

RealProxy()

Initialise une nouvelle instance de la classe RealProxy avec des valeurs par défaut.

RealProxy(Type)

Initialise une nouvelle instance de la classe RealProxy qui représente un objet distant du Type spécifié.

RealProxy(Type, IntPtr, Object)

Initialise une nouvelle instance de la classe RealProxy.

Méthodes

AttachServer(MarshalByRefObject)

Attache l'instance de proxy en cours au MarshalByRefObject distant spécifié.

CreateObjRef(Type)

Crée ObjRef pour le type d'objet spécifié et l'inscrit avec l'infrastructure de communication à distance en tant qu'objet activé par le client.

DetachServer()

Détache l'instance de proxy en cours de l'objet serveur distant qu'elle représente.

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetCOMIUnknown(Boolean)

Demande une référence non managée à l’objet représenté par l’instance de proxy active.

GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetObjectData(SerializationInfo, StreamingContext)

Ajoute au RealProxy spécifié le proxy transparent de l'objet représenté par l'instance actuelle de SerializationInfo.

GetProxiedType()

Retourne Type pour l'objet représenté par l'instance actuelle de RealProxy.

GetStubData(RealProxy)

Récupère les données stub stockées pour le proxy spécifié.

GetTransparentProxy()

Retourne le proxy transparent pour l'instance actuelle de RealProxy.

GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
GetUnwrappedServer()

Retourne l'objet serveur représenté par l'instance de proxy en cours.

InitializeServerObject(IConstructionCallMessage)

Initialise une nouvelle instance du Type d'objet de l'objet distant représenté par l'instance actuelle de RealProxy avec le IConstructionCallMessage spécifié.

Invoke(IMessage)

En cas de substitution dans une classe dérivée, appelle la méthode spécifiée dans le IMessage fourni pour l'objet distant représenté par l'instance actuelle.

MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
SetCOMIUnknown(IntPtr)

Stocke un proxy non managé de l'objet représenté par l'instance actuelle.

SetStubData(RealProxy, Object)

Définit les données stub associées au proxy spécifié.

SupportsInterface(Guid)

Demande une interface COM avec l'ID spécifié.

ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à