Reputation: 1150
Is something like this possible?
//
// create a delegate
Action<Type> action = (t) => t.DoSomething;
//
// get the IL generator for a method
ILGenerator il = myMethodBuilder.GetILGenerator();
//
// try and call the delegate
il.EmitCall(OpCodes.Callvirt, action.Method, null);
Im getting a MethodAccessException whenever I try to invoke the method.
Thanks
Upvotes: 0
Views: 910
Reputation: 7577
If you use a dynamic method you can use the skip visibility checks in the jitter.
Upvotes: 0
Reputation: 54734
Im getting a MethodAccessException whenever I try to invoke the method.
This is because the method generated for the C# (t) => t.DoSomething
lambda is private. Chances are this lambda won't be static, either, depending on which of the local variables it captures from the outer method. You're issuing a callvirt
instruction but you don't appear to be supplying an instance.
You can verify this by loading your application's code in Reflector and looking at the implementation of your (t) => t.DoSomething
lambda.
You need to either:
public static
method in an externally-visible classAction<Type>
in your IL method, generate code that calls Action<Type>.Invoke
, then pass your action
variable into the generated methodUpvotes: 3