1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| public class TestProxy : RealProxy { private IEngineProxy _target;
public TestProxy(Type classToProxy, IEngineProxy target) : base(classToProxy) { _target = target; }
public override IMessage Invoke(IMessage msg) { var methodCall = (IMethodCallMessage)msg; var method = (MethodInfo)methodCall.MethodBase;
try { Console.WriteLine("Before invoke: " + method.Name); var result = method.Invoke(_target, methodCall.InArgs); Console.WriteLine("After invoke: " + method.Name); return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall); } catch (Exception e) { Console.WriteLine("Exception: " + e); if (e is TargetInvocationException && e.InnerException != null) { return new ReturnMessage(e.InnerException, msg as IMethodCallMessage); }
return new ReturnMessage(e, msg as IMethodCallMessage); } } }
public class MyRealProxy<T> : RealProxy where T : class { private T _target;
public MyRealProxy(T target) : base(typeof(T)) { _target = target; }
public static T Create(T instance) { return (T)new MyRealProxy<T>(instance).GetTransparentProxy(); } public override IMessage Invoke(IMessage msg) { try { IMethodCallMessage call = msg as IMethodCallMessage; var args = call.Args; var interfaces = call.GetType().GetInterfaces(); object returnValue = typeof(MarshaleLoader).InvokeMember(call.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, _target, args); return new ReturnMessage(returnValue, args, args.Length, call.LogicalCallContext, call); } catch (Exception e) { throw e; } } }
|