public void Generate(MethodInfo method) { if (method.IsGenericMethod) { method = method.GetGenericMethodDefinition(); } var parameters = method.GetParameters(); var parameterTypes = parameters.Select(parameter => parameter.ParameterType).ToArray(); var methodBuilder = _wrapperBuilder.DefineMethod( method.Name, MethodAttributes.Public | MethodAttributes.Virtual, method.ReturnType, parameterTypes); if (method.IsGenericMethod) { methodBuilder.DefineGenericParameters( method.GetGenericArguments().Select(arg => arg.Name).ToArray()); } ILGenerator ilGenerator = methodBuilder.GetILGenerator(); // Push ModelHolder onto the stack // Call ModelHolder ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Call, typeof(ModelBase).GetProperties().Where(x => x.Name == "ModelHolder").First().GetMethod); // Push the function name on the stack ilGenerator.Emit(OpCodes.Ldstr, DynamicWrapper.MethodSerialize(method)); //ilGenerator.Emit(OpCodes.Ldstr, method.Name); // Push object[] paramseters // Create object[] array ilGenerator.Emit(OpCodes.Ldc_I4_S, parameters.Length); ilGenerator.Emit(OpCodes.Newarr, typeof(object)); // Set values in array for (int i = 0; i < parameters.Length; i++) { ilGenerator.Emit(OpCodes.Dup); ilGenerator.Emit(OpCodes.Ldc_I4_S, i); Type type = parameters[i].ParameterType; ilGenerator.Emit(OpCodes.Ldarg, i + 1); if (type.IsPrimitive) { ilGenerator.Emit(OpCodes.Box, type); } //Put stack value into array position ilGenerator.Emit(OpCodes.Stelem_Ref); } var methodCall = typeof(IModelHolder).GetMethod("MethodCall"); ilGenerator.EmitCall(OpCodes.Callvirt, methodCall, new Type[] { typeof(string), typeof(object[]) }); if (method.ReturnType != typeof(void)) { if (method.ReturnType.IsPrimitive) { ilGenerator.Emit(OpCodes.Unbox_Any, method.ReturnType); } else if (method.ReturnType != typeof(object)) { ilGenerator.Emit(OpCodes.Castclass, method.ReturnType); } } else { ilGenerator.Emit(OpCodes.Pop); } ilGenerator.Emit(OpCodes.Ret); }
public FileSync(Stream stream) { remoteClientSync = DynamicWrapper.CreateClientModel <IFileSync>(new CallWriter(stream, typeof(IFileSync))); }
static void Main(string[] args) { /* * BetterStream stream = new BetterStream(); * string dir = @"C:/subsync/"; * var fileSync = new FileSync(stream); * * fileSync.SyncFiles(dir, new string[] { "*.csproj" }); * * return; */ var LocalHost = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(LocalHost, 0); listener.Start(); Task.Run(() => { var client = listener.AcceptTcpClient(); var stream = client.GetStream(); var serverModel = new CallReader(stream, new Magic()); }); int port = ((IPEndPoint)listener.LocalEndpoint).Port; { TcpClient client = new TcpClient(); client.Connect(LocalHost, port); var stream = client.GetStream(); var model = DynamicWrapper.CreateClientModel <IMagic>(new CallWriter(stream, typeof(IMagic))); var result = model.Test(5, 5342948023980934820L, "test"); Console.WriteLine(result); model.Test(-234, -1L, "test5"); //var body = model.GetType().GetMethods().First(x => x.Name == "Test").GetMethodBody(); var result3 = model.TakeList(new List <int>() { 1, 2, 3, 3 }); Console.WriteLine(string.Join(", ", result3)); var result2 = model.TakeObj(new ObjectObj() { x = 5 }); Console.WriteLine(result2.x); new Thread(() => { Thread.Sleep(1000); model.Test(5, 5, "asfasdfafsd"); }).Start(); } //model.Test(test, 1, "str"); //model.Test(2, 5, "str2"); Console.ReadKey(); return; //string dir = @"\\10.10.84.84\e\scratch\speed\"; //string dir = @"c:\scratch\speed\"; string dir = @"C:/Users/quentin.brooks/Dropbox"; dir = @"C:/"; /* * //string dir = @"C:\Users\quentin.brooks\Dropbox\Proxy"; * * FindAll(EnumerateRoot(dir, new string[] { "*.csproj" })); * * List<string> files = new List<string>(); * using (new Time()) * { * EnumerateDirect(dir, files, new string[] { "*.csproj" }); * } * Console.WriteLine("Found " + files.Count); */ //FindAll(Directory.EnumerateFiles(dir, "*.csproj", SearchOption.AllDirectories)); //Enumerate(dir); /* * //Directory.GetDirectories() * Directory.EnumerateDirectories(@"\\10.10.84.84\e\git\public_web_sites\www.zazzle.com\", "*", SearchOption.TopDirectoryOnly).AsParallel() * .ForAll(file => * { * Console.WriteLine(file); * }); * * Console.WriteLine("done"); * WatchPath(@"\\10.10.84.84\e\git\public_web_sites\www.zazzle.com", new string[] { "*.ts", "*.tsx" }, change => * { * }); */ Console.WriteLine("DONE"); Console.Read(); }
public CallReader(Stream stream, object instance) { this.stream = stream; this.instance = instance; methods = instance.GetType().AllMethods().Where(x => !x.IsAbstract).ToDictionary(x => DynamicWrapper.MethodSerialize(x), x => new FastMethodInfo(x)); returnTypes = instance.GetType().AllMethods().Where(x => !x.IsAbstract).ToDictionary(x => DynamicWrapper.MethodSerialize(x), x => x.ReturnType); new Thread(ReadLoop).Start(); }