I have a System.Reflection.MethodInfo and would like to have a method that creates a delegate(preferably a Func<...> or an Action<...>) that represents that method, given an instance to invoke it on.
So ideally I would like something like the following psuedo-code:
public TDelegate GetMethod<TDelegate>(MethodInfo methodToRepresent, object instanceToInvokeOn)
{
return (TDelegate)((parameters....) => methodToRepresent.Invoke(instanceToInvokeOn, all parameters in an object[]));
}
where TDelegate represents the signature of the represented method. If the signatures don't match, an exception should be thrown.
I realise I probably can't achieve this with a simple lambda expression, since its parametertypes must be known at compile-time. Perhaps I need to construct a delegate from scratch? Is it possible to create a delegate by specifying its body and parameters seperately?
Thank you