using System.Linq.Expressions;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
public static void Insert<t>(ref t[] ar, t Value, int Position)
System.Array.Resize(ref ar, ar.Length + 1);
if (Position == ar.Length - 1)
ar, Position + 1, ar.Length - Position - 1);
public static Delegate CreateDelagate(
Func<object[], object> Func,
var ParamsLength = Params.Length;
if (Result != typeof(void))
Insert(ref Params, typeof(Func<object[], object>), 0);
Insert(ref Params, typeof(Action<object[]>), 0);
var methodName = "CreateInstance_";
var dynMethod = new DynamicMethod(methodName, Result, Params, true);
ILGenerator il = dynMethod.GetILGenerator();
var local = il.DeclareLocal(typeof(object[]));
il.Emit(OpCodes.Ldc_I4, ParamsLength);
il.Emit(OpCodes.Newarr, typeof(object));
invoke = typeof(Func<object[], object>).GetMethod("Invoke");
invoke = typeof(Action<object[]>).GetMethod("Invoke");
for (int i = 1; i < ParamsLength + 1; i++)
il.Emit(OpCodes.Ldc_I4_S, x);
il.Emit(OpCodes.Ldarg_S, i);
il.Emit(OpCodes.Box, Params[i]);
il.Emit(OpCodes.Stelem_Ref);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Call, invoke);
il.Emit(OpCodes.Unbox_Any, Result);
return dynMethod.CreateDelegate(DelegateType, Func);
return dynMethod.CreateDelegate(DelegateType, Action);
public static Delegate CreateDelagate(
Func<object[], object> Func,
Method.GetParameters().Select((c) => c.ParameterType).ToArray(),
Method.ReturnType, Action, Func, DelegateType);
public static void Main()
Action<string, string, string> Server_AC = (c1, c2, c3) =>
Console.WriteLine(c1 + c2 + c3);
var Interface = CreateDelagate(Server_AC.Method, (c) =>
Server_AC.DynamicInvoke(c);
Action<string, string, string> Client =(Action<string, string, string>) Interface;
Client.Invoke("a","b","c");