Consider the following code:
bool result;
Func<int, bool> lambda1 = i => i == 9000;
MethodInfo lambda1Method = lambda1.Method;
result = (bool)lambda1Method.Invoke(null, new object[] { 9000 }); // this works, result = true
int myLocalVariable = 9000;
Func<int, bool> lambda2 = i => i == myLocalVariable;
MethodInfo lambda2Method = lambda2.Method;
result = (bool)lambda2Method.Invoke(null, new object[] { 9000 }); // error
Invoking lambda2Method
results in a System.Reflection.TargetException
:
Non-static method requires a target.
This question here explains why the lambda1Method
is static, while lambda2Method
is non-static. Basically if lambdas contain local variables, a class is dynamically created that interprets each local variable as a field. lambda2Method
becomes an instance method of that new class. I know this because lambda2Method.DeclaringType
is <>c__DisplayClass1
, and lambda2Method.IsStatic
is false
.
My question is, how can I make this work? I understand that because lambda2Method
is non-static, I need to supply a value for the object obj
parameter of MethodBase.Invoke()
, and it needs to be an instance of <>c__DisplayClass1
, but how do I obtain this instance?