I need to be able to pass an arbitrary method to some function myFunction
:
void myFunction(AnyFunc func) { ... }
It should be possible to execute it with other static, instance, public or private methods or even delegates:
myFunction(SomeClass.PublicStaticMethod);
myFunction(SomeObject.PrivateInstanceMethod);
myFunction(delegate(int x) { return 5*x; });
Passed method may have any number of parameters and any return type. It should also be possible to learn the actual number of parameters and their types in myFunction
via reflection. What would be AnyFunc
in the myFunction
definition to accommodate such requirements? It is acceptible to have several overloaded versions of the myFunction
.
The
Delegate
type is the supertype of all other delegate types:Then,
func.Method
will give you aMethodInfo
object you can use to inspect the return type and parameter types.When calling the function you will have to explicitly specify which type of delegate you want to create:
Some idea of what you're trying to accomplish at a higher level would be good, as this approach may not turn out to be ideal.
Have the method accept a
Delegate
, rather than a particular delegate: