Pass method created with reflection as parameter Func

I have a method (fyi, I use C #) accepting a parameter of type "Func", let it be defined as such:

MethodAcceptingFuncParam(Func<bool> thefunction);

I defined a function to pass as such:

public bool DoStuff()
{
    return true;
}

I can easily call it the following:

MethodAcceptingFuncParam(() =>  { return DoStuff(); });

It works as it should, how good it is.

Now, instead of passing the DoStuff () method, I would like to create this method through reflection and pass this to:

Type containingType = Type.GetType("Namespace.ClassContainingDoStuff");
MethodInfo mi = containingType.GetMethod("DoStuff");

=> This works, I can get the info method correctly.

But here is where I am stuck: now I would like to do something like

MethodAcceptingFuncParam(() => { return mi.??? });

In other words, I would like to pass on the method that I just got through reflection as the value for the Func parameter of MethodAcceptingFuncParam. Any tips on how to achieve this?

+5
2

Delegate.CreateDelegate, .

:

var func = (Func<bool>) Delegate.CreateDelegate(typeof(Func<bool>), mi);
MethodAcceptingFuncParam(func);

, MethodAcceptingFuncParam, , mi.Invoke .

+9

Invoke:

MethodAcceptingFuncParam(() => { return (bool)mi.Invoke(null, null); })
+1

All Articles