static void Main(string[] args) { // instantiate my proxy MyProxy proxy = new MyProxy(typeof(IServer), new MyServer()); IServer srvr = (IServer)proxy.GetTransparentProxy(); // use the server foreach (var val in srvr.doSomething("aaa")) { Console.WriteLine("got: {0}", val); } }
public override IMessage Invoke(IMessage message) { IMessage result = null; IMethodCallMessage methodCall = message as IMethodCallMessage; MethodInfo method = methodCall.MethodBase as MethodInfo; // Invoke if (result == null) { if (proxyTarget != null) { Console.WriteLine("proxy going to invoke: {0}", method.Name); object callResult; object actualresult; bool make_proxy = true; if (method.ReturnType.IsInterface) { actualresult = method.Invoke(proxyTarget, methodCall.InArgs); if (method.ReturnType.IsGenericType) { // Console.WriteLine("** return value is generic type: {0}", method.ReturnType.GetGenericTypeDefinition()); if (method.ReturnType.GetGenericTypeDefinition() == (typeof(IEnumerator<>))) { Console.WriteLine("** method returning IEnumerator<>, making BatchProxy"); Type[] args = method.ReturnType.GetGenericArguments(); Type srvbatchtype = typeof(EnumeratorServerBatch<>).MakeGenericType(args); object srv = Activator.CreateInstance(srvbatchtype, actualresult); Type clbatchtype = typeof(EnumeratorClientBatch<>).MakeGenericType(args); object client = Activator.CreateInstance(clbatchtype, srv); make_proxy = false; actualresult = client; } } if (make_proxy) { var newproxy = new MyProxy(method.ReturnType, actualresult); callResult = newproxy.GetTransparentProxy(); } else { callResult = actualresult; } } else { callResult = method.Invoke(proxyTarget, methodCall.InArgs); } Console.WriteLine("proxy done Invoking: {0}", method.Name); LogicalCallContext context = methodCall.LogicalCallContext; result = new ReturnMessage(callResult, null, 0, context, message as IMethodCallMessage); } else { NotSupportedException exception = new NotSupportedException("proxyTarget is not defined"); result = new ReturnMessage(exception, message as IMethodCallMessage); } } return result; }