![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Imessage例子
1
// Create a custom 'RealProxy'.
2
public class MyProxy : RealProxy
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
String myURIString;
5
MarshalByRefObject myMarshalByRefObject;
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
[PermissionSet(SecurityAction.LinkDemand)]
8
public MyProxy(Type myType) : base(myType)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
20
public override IMessage Invoke(IMessage myIMessage)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
Console.WriteLine("MyProxy.Invoke Start");
23
Console.WriteLine("");
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
if (myIMessage is IMethodCallMessage)
26
Console.WriteLine("IMethodCallMessage");
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
if (myIMessage is IMethodReturnMessage)
29
Console.WriteLine("IMethodReturnMessage");
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
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();
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
while (myIDictionaryEnumerator.MoveNext())
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
Object myKey = myIDictionaryEnumerator.Key;
43
String myKeyName = myKey.ToString();
44
Object myValue = myIDictionaryEnumerator.Value;
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
Console.WriteLine("\t{0} : {1}", myKeyName,
47
myIDictionaryEnumerator.Value);
48
if (myKeyName == "__Args")
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
}
55![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
if ((myKeyName == "__MethodSignature") && (null != myValue))
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
myIDictionary["__Uri"] = myURIString;
68
Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
Console.WriteLine("ChannelServices.SyncDispatchMessage");
71
myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
// Push return value and OUT parameters back onto stack.
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)
76
myReturnMessage;
77
Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}",
78
myMethodReturnMessage.ReturnValue);
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
Console.WriteLine("MyProxy.Invoke - Finish");
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
return myReturnMessage;
83
}
84
}
posted @
2006-12-20 13:01
南守拥
阅读(
922)
评论()
编辑
收藏
举报