此代码:
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); generator.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
时,我做错了什么?