0

这段代码:

var returnType = GetDelegateReturnType(typeof(TimerCallback));
var methodParameters = typeof(TimerCallback).GetMethod("Invoke").GetParameters().Select(x => x.ParameterType).ToArray();

var handler = new System.Reflection.Emit.DynamicMethod("", returnType, methodParameters, typeof(ArgumentNullTest));
var generator = handler.GetILGenerator();

generator.Emit(System.Reflection.Emit.OpCodes.Ldloc, 1);
生成器.Emit(System.Reflection.Emit.OpCodes.Ret);

var timerCallback = (TimerCallback)handler.CreateDelegate(typeof(TimerCallback));
var timer = new Timer(timerCallback, null, 0, 0);
var timer2 = new Timer(o => { }, null, 0, 0);

InvalidProgramException创建第二个计时器时抛出一个。TimerCallback创建每个反射时我做错了什么?

4

0 回答 0