IMessage接口初接触
1
// Create a custom 'RealProxy'.2
public class MyProxy : RealProxy3


{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
}18

19
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]20
public override IMessage Invoke(IMessage myIMessage)21

{22
Console.WriteLine("MyProxy.Invoke Start");23
Console.WriteLine("");24

25
if (myIMessage is IMethodCallMessage)26
Console.WriteLine("IMethodCallMessage");27

28
if (myIMessage is IMethodReturnMessage)29
Console.WriteLine("IMethodReturnMessage");30

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

40
while (myIDictionaryEnumerator.MoveNext())41

{42
Object myKey = myIDictionaryEnumerator.Key;43
String myKeyName = myKey.ToString();44
Object myValue = myIDictionaryEnumerator.Value;45

46
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
}55

56
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;66

67
myIDictionary["__Uri"] = myURIString;68
Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);69

70
Console.WriteLine("ChannelServices.SyncDispatchMessage");71
myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);72

73
// Push return value and OUT parameters back onto stack.74

75
IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)76
myReturnMessage;77
Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}", 78
myMethodReturnMessage.ReturnValue);79

80
Console.WriteLine("MyProxy.Invoke - Finish");81

82
return myReturnMessage;83
}84
}

浙公网安备 33010602011771号