This seems to work, providing a (weird) way to call an Action:
Action action = () => { };
action.Method.Invoke(action.Target, new object[0]);
This seems to work, providing a (helpful) way to create an Action:
var action = dynamicMethod.CreateDelegate(typeof(Action)) as Action;
action();
However, this throws an Exception:
var action = dynamicMethod.CreateDelegate(typeof(Action)) as Action;
action.Method.Invoke(action.Target, new object[0]); // Throws exception
MethodInfo must be a runtime MethodInfo object.
Question: Why is the above code snippet throwing an Exception?
Working code example
var dynamicMethod = new System.Reflection.Emit.DynamicMethod(
""
, typeof(void)
, new Type[0]
);
var ilGenerator = dynamicMethod.GetILGenerator();
ilGenerator.Emit(System.Reflection.Emit.OpCodes.Ret);
var action = dynamicMethod.CreateDelegate(typeof(Action)) as Action;
try
{
action.Method.Invoke(action.Target, new object[0]);
}
catch (Exception exception)
{
System.Console.WriteLine(exception);
}
This causes the Console to write:
Exception thrown: 'System.ArgumentException' in mscorlib.dll
System.ArgumentException: MethodInfo must be a runtime MethodInfo object.
Parameter name: this
at System.Reflection.Emit.DynamicMethod.RTDynamicMethod.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
[...]
Thoughts
I've tried a bunch of variations on calling action.Method.Invoke(), but all sorts of variations on the call arguments don't seem to change the exception-message,
MethodInfo must be a runtime MethodInfo object.
My guess is that action.Method isn't a "runtime MethodInfo", despite being a "MethodInfo". I'm not really sure what the distinction between a runtime-MethodInfo and a non-runtime-MethodInfo might be, though.