You can achieve a certain amount of strong input - by repeating with Func options:
public R InvokeMethod<T,R>(Func<T,R> method, T argument) { return method(argument); } public R InvokeMethod<T1,T2,R>(Func<T1,T2,R> method, T1 argument1, T2 argument2) { return method(argument1, argument2); } public R InvokeMethod<T1,T2,T3,R>(Func<T1,T2,T3,R> method, T1 argument1, T2 argument2, T3 argument3) { return method(argument1, argument2, argument3); }
And so on.
Although this is consistent with your original, there is no real need to handle the parameters at all. Try writing InvokeMethod as follows:
public R InvokeMethod<R>(Func<R> method) { return method(); }
and then call it in this style:
internal bool RemoteLogin(string password) { return InvokeMethod(() => Server.RemoteLogin(password)); } internal string GetSessionId() { return InvokeMethod( () => Server.GetSessionId()); }
That way you leave parameter processing with the lambda expression, and you only need to write InvokeMethod once.
source share