Share via


IMethodMessage 接口

定义方法消息接口。

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

语法

声明
<ComVisibleAttribute(True)> _
Public Interface IMethodMessage
    Inherits IMessage
用法
Dim instance As IMethodMessage
[ComVisibleAttribute(true)] 
public interface IMethodMessage : IMessage
[ComVisibleAttribute(true)] 
public interface class IMethodMessage : IMessage
/** @attribute ComVisibleAttribute(true) */ 
public interface IMethodMessage extends IMessage
ComVisibleAttribute(true) 
public interface IMethodMessage extends IMessage

备注

方法消息用于将信息发送到远程方法以及从远程方法发送信息。例如,用于远程方法调用的消息实现 IMethodMessage 接口。

示例

下面的代码示例显示了一个重写 RealProxy.Invoke 的自定义代理,目的是将消息信息写入到控制台,并且不进行远程调用就立即返回。

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class MyProxyClass
   Inherits RealProxy
   Private myObjectInstance As Object = Nothing
   Private myType As Type = Nothing
   
   Public Sub New(argType As Type)
      MyBase.New(argType)
      myType = argType
      myObjectInstance = Activator.CreateInstance(argType)
   End Sub 'New
   
   ' Overriding the Invoke method of RealProxy.
   Public Overrides Function Invoke(message As IMessage) As IMessage
      Dim myMethodMessage As IMethodMessage = CType(message, IMethodMessage)
      
      Console.WriteLine("**** Begin Invoke ****")
      Console.WriteLine(ControlChars.Tab + "Type is : " + myType.ToString())
      Console.WriteLine(ControlChars.Tab + "Method name : " + myMethodMessage.MethodName)
      
      Dim i As Integer
      For i = 0 To myMethodMessage.ArgCount - 1
         Console.WriteLine(ControlChars.Tab + "ArgName is : " + myMethodMessage.GetArgName(i))
         Console.WriteLine(ControlChars.Tab + "ArgValue is: " + myMethodMessage.GetArg(i))
      Next i
      
      If myMethodMessage.HasVarArgs Then
         Console.WriteLine(ControlChars.Tab + " The method have variable arguments!!")
      Else
         Console.WriteLine(ControlChars.Tab + " The method does not have variable arguments!!")
      End If 
      ' Dispatch the method call to the real object.
      Dim returnValue As Object = myType.InvokeMember(myMethodMessage.MethodName, _
                     BindingFlags.InvokeMethod, Nothing, myObjectInstance, myMethodMessage.Args)
      Console.WriteLine("**** End Invoke ****")
      
      ' Build the return message to pass back to the transparent proxy.
      Dim myReturnMessage As New ReturnMessage(returnValue, Nothing, 0, Nothing, _
                                                         CType(message, IMethodCallMessage))
      Return myReturnMessage
   End Function 'Invoke
End Class 'MyProxyClass
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public class MyProxyClass : RealProxy
{
   private Object  myObjectInstance  = null;
   private Type    myType      = null;

   public MyProxyClass(Type argType) : base(argType)
   {
      myType = argType;
      myObjectInstance = Activator.CreateInstance(argType);
   }

   // Overriding the Invoke method of RealProxy.
   public override IMessage Invoke(IMessage message)
   {
      IMethodMessage myMethodMessage = (IMethodMessage)message;

      Console.WriteLine("**** Begin Invoke ****");
      Console.WriteLine("\tType is : " + myType);
      Console.WriteLine("\tMethod name : " +  myMethodMessage.MethodName);

      for (int i=0; i < myMethodMessage.ArgCount; i++)
      {
         Console.WriteLine("\tArgName is : " + myMethodMessage.GetArgName(i));
         Console.WriteLine("\tArgValue is: " + myMethodMessage.GetArg(i));
      }

      if(myMethodMessage.HasVarArgs)
          Console.WriteLine("\t The method have variable arguments!!");
      else
          Console.WriteLine("\t The method does not have variable arguments!!");

      // Dispatch the method call to the real object.
      Object returnValue = myType.InvokeMember( myMethodMessage.MethodName, BindingFlags.InvokeMethod, null,
                                           myObjectInstance, myMethodMessage.Args );
      Console.WriteLine("**** End Invoke ****");

      // Build the return message to pass back to the transparent proxy.
      ReturnMessage myReturnMessage = new ReturnMessage( returnValue, null, 0, null,
          (IMethodCallMessage)message );
      return myReturnMessage;
   }
}
// This class requires full trust
[PermissionSetAttribute(SecurityAction::Demand, Name = "FullTrust")]
public ref class MyProxyClass: public RealProxy
{
private:
   Object^ myObjectInstance;
   Type^ myType;

public:
   MyProxyClass( Type^ argType )
      : RealProxy( argType )
   {
      myType = argType;
      myObjectInstance = Activator::CreateInstance( argType );
   }

   // Overriding the Invoke method of RealProxy.
   virtual IMessage^ Invoke( IMessage^ message ) override
   {
      IMethodMessage^ myMethodMessage = dynamic_cast<IMethodMessage^>(message);
      Console::WriteLine( "**** Begin Invoke ****" );
      Console::WriteLine( "\tType is : {0}", myType );
      Console::WriteLine( "\tMethod name : {0}", myMethodMessage->MethodName );
      for ( int i = 0; i < myMethodMessage->ArgCount; i++ )
      {
         Console::WriteLine( "\tArgName is : {0}", myMethodMessage->GetArgName( i ) );
         Console::WriteLine( "\tArgValue is: {0}", myMethodMessage->GetArg( i ) );

      }
      if ( myMethodMessage->HasVarArgs )
            Console::WriteLine( "\t The method have variable arguments!!" );
      else
            Console::WriteLine( "\t The method does not have variable arguments!!" );

      
      // Dispatch the method call to the real Object*.
      Object^ returnValue = myType->InvokeMember( myMethodMessage->MethodName, BindingFlags::InvokeMethod, nullptr, myObjectInstance, myMethodMessage->Args );
      Console::WriteLine( "**** End Invoke ****" );
      
      // Build the return message to pass back to the transparent proxy.
      ReturnMessage^ myReturnMessage = gcnew ReturnMessage( returnValue,nullptr,0,nullptr,dynamic_cast<IMethodCallMessage^>(message) );
      return myReturnMessage;
   }
};

平台

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

请参见

参考

IMethodMessage 成员
System.Runtime.Remoting.Messaging 命名空间