This code works
var toString = typeof(string).GetMethod("ToString", new Type[] { });
var dm = new DynamicMethod("MyToString", typeof(string), new Type[] { typeof(string) });
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, toString);
il.Emit(OpCodes.Ret);
Delegate d = dm.CreateDelegate(typeof(Func<string, string>));
var r = d.DynamicInvoke("10");
This code throws an exception (System.Security.VerificationException: Operation could destabilize the runtime.)
var toString = typeof(int).GetMethod("ToString", new Type[] { });
var dm = new DynamicMethod("MyToString", typeof(string), new Type[] { typeof(int) });
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, toString);
il.Emit(OpCodes.Ret);
Delegate d = dm.CreateDelegate(typeof(Func<int, string>));
var r = d.DynamicInvoke(10);
why?
Aucun commentaire:
Enregistrer un commentaire