private void ExecHandlerByEmit(IArgsHandler handler, IArgs args) { Type argType = handler.GetType().GetInterfaces() .FirstOrDefault(x => x.IsGenericType) .GetGenericArguments()[0]; DynamicMethod dynamicMethod = new DynamicMethod("TriggerMethod", null, new Type[] { typeof(IArgsHandler), typeof(IArgs) }); var il = dynamicMethod.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Castclass, handler.GetType()); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Castclass, argType); il.Emit(OpCodes.Callvirt, handler.GetType().GetMethod("Handle")); il.Emit(OpCodes.Ret); var act = (Action <IArgsHandler, IArgs>)dynamicMethod.CreateDelegate(typeof(Action <IArgsHandler, IArgs>)); act(handler, args); }
private void ExecHandle(IArgsHandler handler, IArgs args) { handler.GetType().GetMethod("Handle").Invoke(handler, new object[] { args }); }