IMessage接口初接触

it2022-05-09  28

Imessage例子  1// Create a custom 'RealProxy'. 2public class MyProxy : RealProxy 3{ 4   String myURIString; 5   MarshalByRefObject myMarshalByRefObject;    6 7   [PermissionSet(SecurityAction.LinkDemand)] 8   public MyProxy(Type myType) : base(myType) 9   {10      // RealProxy uses the Type to generate a transparent proxy.11      myMarshalByRefObject = (MarshalByRefObject)Activator.CreateInstance((myType));12      // Get 'ObjRef', for transmission serialization between application domains.13      ObjRef myObjRef = RemotingServices.Marshal(myMarshalByRefObject);14      // Get the 'URI' property of 'ObjRef' and store it.15      myURIString = myObjRef.URI;16      Console.WriteLine("URI :{0}", myObjRef.URI);17   }1819   [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]20   public override IMessage Invoke(IMessage myIMessage)21   {22      Console.WriteLine("MyProxy.Invoke Start");23      Console.WriteLine("");2425      if (myIMessage is IMethodCallMessage)26         Console.WriteLine("IMethodCallMessage");2728      if (myIMessage is IMethodReturnMessage)29         Console.WriteLine("IMethodReturnMessage");3031      Type msgType = myIMessage.GetType();32      Console.WriteLine("Message Type: {0}", msgType.ToString());33      Console.WriteLine("Message Properties");34      IDictionary myIDictionary = myIMessage.Properties;35      // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.36      myIDictionary["__Uri"= myURIString;37      IDictionaryEnumerator myIDictionaryEnumerator = 38         (IDictionaryEnumerator) myIDictionary.GetEnumerator();3940      while (myIDictionaryEnumerator.MoveNext())41      {42         Object myKey = myIDictionaryEnumerator.Key;43         String myKeyName = myKey.ToString();44         Object myValue = myIDictionaryEnumerator.Value;4546         Console.WriteLine("\t{0} : {1}", myKeyName, 47            myIDictionaryEnumerator.Value);48         if (myKeyName == "__Args")49         {50            Object[] myObjectArray = (Object[])myValue;51            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)52               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, 53                  myObjectArray[aIndex]);54         }5556         if ((myKeyName == "__MethodSignature"&& (null != myValue))57         {58            Object[] myObjectArray = (Object[])myValue;59            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)60               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, 61                  myObjectArray[aIndex]);62         }63      }64      65      IMessage myReturnMessage;6667      myIDictionary["__Uri"= myURIString;68      Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);6970      Console.WriteLine("ChannelServices.SyncDispatchMessage");71      myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);7273      // Push return value and OUT parameters back onto stack.7475      IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)76         myReturnMessage;77      Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}"78         myMethodReturnMessage.ReturnValue);7980      Console.WriteLine("MyProxy.Invoke - Finish");8182      return myReturnMessage;83   }84}

转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/12/20/597881.html

相关资源:数据结构—成绩单生成器

最新回复(0)