IMessage接口初接触

ContractedBlock.gif ExpandedBlockStart.gif Imessage例子
 1None.gif// Create a custom 'RealProxy'.
 2None.gifpublic class MyProxy : RealProxy
 3ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 4InBlock.gif   String myURIString;
 5InBlock.gif   MarshalByRefObject myMarshalByRefObject;   
 6InBlock.gif
 7InBlock.gif   [PermissionSet(SecurityAction.LinkDemand)]
 8InBlock.gif   public MyProxy(Type myType) : base(myType)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
10InBlock.gif      // RealProxy uses the Type to generate a transparent proxy.
11InBlock.gif      myMarshalByRefObject = (MarshalByRefObject)Activator.CreateInstance((myType));
12InBlock.gif      // Get 'ObjRef', for transmission serialization between application domains.
13InBlock.gif      ObjRef myObjRef = RemotingServices.Marshal(myMarshalByRefObject);
14InBlock.gif      // Get the 'URI' property of 'ObjRef' and store it.
15InBlock.gif      myURIString = myObjRef.URI;
16InBlock.gif      Console.WriteLine("URI :{0}", myObjRef.URI);
17ExpandedSubBlockEnd.gif   }

18InBlock.gif
19InBlock.gif   [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
20InBlock.gif   public override IMessage Invoke(IMessage myIMessage)
21ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
22InBlock.gif      Console.WriteLine("MyProxy.Invoke Start");
23InBlock.gif      Console.WriteLine("");
24InBlock.gif
25InBlock.gif      if (myIMessage is IMethodCallMessage)
26InBlock.gif         Console.WriteLine("IMethodCallMessage");
27InBlock.gif
28InBlock.gif      if (myIMessage is IMethodReturnMessage)
29InBlock.gif         Console.WriteLine("IMethodReturnMessage");
30InBlock.gif
31InBlock.gif      Type msgType = myIMessage.GetType();
32InBlock.gif      Console.WriteLine("Message Type: {0}", msgType.ToString());
33InBlock.gif      Console.WriteLine("Message Properties");
34InBlock.gif      IDictionary myIDictionary = myIMessage.Properties;
35InBlock.gif      // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
36InBlock.gif      myIDictionary["__Uri"= myURIString;
37InBlock.gif      IDictionaryEnumerator myIDictionaryEnumerator = 
38InBlock.gif         (IDictionaryEnumerator) myIDictionary.GetEnumerator();
39InBlock.gif
40InBlock.gif      while (myIDictionaryEnumerator.MoveNext())
41ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
42InBlock.gif         Object myKey = myIDictionaryEnumerator.Key;
43InBlock.gif         String myKeyName = myKey.ToString();
44InBlock.gif         Object myValue = myIDictionaryEnumerator.Value;
45InBlock.gif
46InBlock.gif         Console.WriteLine("\t{0} : {1}", myKeyName, 
47InBlock.gif            myIDictionaryEnumerator.Value);
48InBlock.gif         if (myKeyName == "__Args")
49ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
50InBlock.gif            Object[] myObjectArray = (Object[])myValue;
51InBlock.gif            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
52InBlock.gif               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, 
53InBlock.gif                  myObjectArray[aIndex]);
54ExpandedSubBlockEnd.gif         }

55InBlock.gif
56InBlock.gif         if ((myKeyName == "__MethodSignature"&& (null != myValue))
57ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
58InBlock.gif            Object[] myObjectArray = (Object[])myValue;
59InBlock.gif            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
60InBlock.gif               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, 
61InBlock.gif                  myObjectArray[aIndex]);
62ExpandedSubBlockEnd.gif         }

63ExpandedSubBlockEnd.gif      }

64InBlock.gif      
65InBlock.gif      IMessage myReturnMessage;
66InBlock.gif
67InBlock.gif      myIDictionary["__Uri"= myURIString;
68InBlock.gif      Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);
69InBlock.gif
70InBlock.gif      Console.WriteLine("ChannelServices.SyncDispatchMessage");
71InBlock.gif      myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);
72InBlock.gif
73InBlock.gif      // Push return value and OUT parameters back onto stack.
74InBlock.gif
75InBlock.gif      IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)
76InBlock.gif         myReturnMessage;
77InBlock.gif      Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}"
78InBlock.gif         myMethodReturnMessage.ReturnValue);
79InBlock.gif
80InBlock.gif      Console.WriteLine("MyProxy.Invoke - Finish");
81InBlock.gif
82InBlock.gif      return myReturnMessage;
83ExpandedSubBlockEnd.gif   }

84ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值