110,561
社区成员
发帖
与我相关
我的任务
分享
DynamicMethod method = new DynamicMethod("dynamicMethod", typeof(List<Tuple<string, object>>), new Type[] { typeof(string), typeof(object) });
var il = method.GetILGenerator();
var constructor = typeof(Tuple<,>).MakeGenericType(new Type[] { typeof(string), typeof(object) }).GetConstructor(new Type[] { typeof(string), typeof(object) });
il.DeclareLocal(typeof(List<Tuple<string, object>>));
il.DeclareLocal(typeof(Tuple<string, object>));
il.Emit(OpCodes.Newobj, typeof(List<Tuple<string, object>>).GetConstructor(new Type[0]));
il.Emit(OpCodes.Stloc, 0);
il.Emit(OpCodes.Ldarg, 0);
il.Emit(OpCodes.Ldarg, 1);
il.Emit(OpCodes.Newobj, constructor);
il.Emit(OpCodes.Stloc, 1);
il.Emit(OpCodes.Ldloc, 0);
il.Emit(OpCodes.Ldloc, 1);
il.Emit(OpCodes.Callvirt, typeof(List<Tuple<string, object>>).GetMethod("Add"));
il.Emit(OpCodes.Ldloc, 0);
il.Emit(OpCodes.Ret);
var dm = (Func<string, object, List<Tuple<string, object>>>)method.CreateDelegate(typeof(Func<string, object, List<Tuple<string, object>>>));
var val = dm("我是", new { name = "LEE" });