RealProxy 类

提供代理的基本功能。

**命名空间:**System.Runtime.Remoting.Proxies
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public MustInherit Class RealProxy
用法
Dim instance As RealProxy
[ComVisibleAttribute(true)] 
public abstract class RealProxy
[ComVisibleAttribute(true)] 
public ref class RealProxy abstract
/** @attribute ComVisibleAttribute(true) */ 
public abstract class RealProxy
ComVisibleAttribute(true) 
public abstract class RealProxy

备注

RealProxy 类是代理必须继承的 abstract 基类。

客户端在跨任何类型的远程处理边界使用对象时,对对象使用的实际上是透明代理。透明代理使人以为实际对象驻留在客户端空间中。它实现这一点的方法是:使用远程处理基础结构将对其进行的调用转发给真实对象。

透明代理本身由 RealProxy 类型的托管运行时类的实例收容。RealProxy 实现从透明代理转发操作所需的部分功能。请注意,代理对象继承与托管对象(如垃圾回收、字段和方法支持)关联的语义,并且可以扩展以形成新类。该代理具有双重特性:它作为与远程对象(透明代理)相同的类的对象,同时其本身是托管对象。

可以在不考虑 AppDomain 中任何远程处理分支的情况下使用代理对象。

提示

此类在类级别生成链接要求和继承要求。如果直接调用方或继承类没有基础结构权限,则会引发 SecurityException。有关安全要求的详细信息,请参见 链接要求继承要求

给继承者的说明RealProxy 继承时,必须重写 Invoke 方法。

示例

' 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 'New

<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 'MyProxy
// Create a custom 'RealProxy'.
public class MyProxy : RealProxy
{
   String myURIString;
   MarshalByRefObject myMarshalByRefObject;   

   [PermissionSet(SecurityAction.LinkDemand)]
   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);
   }

   [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
   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'.
/** @attribute SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure) */
public class MyProxy extends RealProxy
{
    private String myURIString;
    private MarshalByRefObject myMarshalByRefObject;

    public MyProxy(Type myType)
    {
        super(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.get_URI();
        Console.WriteLine("URI :{0}", myObjRef.get_URI());
    } //MyProxy

    public IMessage Invoke(IMessage myIMessage)
    {
        Console.WriteLine("MyProxy.Invoke Start");
        Console.WriteLine("");
        if (myIMessage instanceof IMethodCallMessage) {
            Console.WriteLine("IMethodCallMessage");
        }

        if (myIMessage instanceof IMethodReturnMessage) {
            Console.WriteLine("IMethodReturnMessage");
        }

        Type msgType = myIMessage.GetType();

        Console.WriteLine("Message Type: {0}", msgType.ToString());
        Console.WriteLine("Message Properties");

        IDictionary myIDictionary = myIMessage.get_Properties();

        // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
        myIDictionary.set_Item("__Uri", myURIString);

        IDictionaryEnumerator myIDictionaryEnumerator = 
            (IDictionaryEnumerator)(myIDictionary.GetEnumerator());
        while (myIDictionaryEnumerator.MoveNext()) {
            Object myKey = myIDictionaryEnumerator.get_Key();
            String myKeyName = myKey.ToString();
            Object myValue = myIDictionaryEnumerator.get_Value();
            Console.WriteLine("\t{0} : {1}", myKeyName, 
                myIDictionaryEnumerator.get_Value());

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

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

        IMessage myReturnMessage;
        myIDictionary.set_Item("__Uri", myURIString);
        Console.WriteLine("__Uri {0}", myIDictionary.get_Item("__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.get_ReturnValue());
        Console.WriteLine("MyProxy.Invoke - Finish");
        return myReturnMessage;
    } //Invoke
} //MyProxy

.NET Framework 安全性

继承层次结构

System.Object
  System.Runtime.Remoting.Proxies.RealProxy

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

RealProxy 成员
System.Runtime.Remoting.Proxies 命名空间