Created
May 31, 2011 19:10
-
-
Save svick/1001085 to your computer and use it in GitHub Desktop.
Mock of how could you call a method on another computer using DynamicProxy and pseudo-JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Castle.DynamicProxy; | |
namespace AnotherContinent | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var instance = CreateInstance<IComparable<int>>(); | |
Console.WriteLine(instance.CompareTo(25)); | |
} | |
public static T CreateInstance<T>() where T : class | |
{ | |
return new ProxyGenerator().CreateInterfaceProxyWithoutTarget<T>(new ConsoleInterceptor<T>()); | |
} | |
} | |
class ConsoleInterceptor<T> : IInterceptor | |
{ | |
public void Intercept(IInvocation invocation) | |
{ | |
var arguments = invocation.Method.GetParameters() | |
.Select((t, i) => string.Format("{0} : \"{1}\"", t.Name, invocation.Arguments[i].ToString())) | |
.ToList(); | |
string result = string.Format("{{ type: \"{0}\", method: \"{1}\", arguments: {{ {2} }} }}", | |
typeof(T).Name, invocation.Method.Name, | |
string.Join(", ", arguments)); | |
Console.WriteLine(result); | |
Type returnType = invocation.Method.ReturnType; | |
if (returnType != typeof(void)) | |
if (returnType.IsValueType) | |
invocation.ReturnValue = Activator.CreateInstance(returnType); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment