One thing you can do is create several different methods (one for methods without arguments, one for methods with one argument, etc.), for example:
public static void DisplayForm(Action action) {
DisplayFormUsingInvoke(action);
}
public static void DisplayForm<T>(Action<T> action, T param) {
DisplayFormUsingInvoke(action, param);
}
public static void DisplayForm<T,U>(Action<T,U> action, T param1, U param2) {
DisplayFormUsingInvoke(action, param1, param2);
}
...
Then you can have a private method that actually does the job, similar to your current method, but not exposed to the client:
private static void DisplayFormUsingInvoke(Delegate d, params object[] parms) {
d.DynamicInvoke(parms);
}
Then the client code will call one of the public methods, which will ensure that the correct number and types of arguments are specified.
source
share