So I'm attempting to be able to pass a Func with a variable number of parameters.
Something like:
public object GetValue<T>(string name, Func<object> func) {
var result = func.DynamicInvoke();
}
The above function/signature works great when the number of arguments to func is know. But it breaks down quickly when you want the number of arguments to be unknown until runtime.
I'd like to change the method signature to allow for the following scenarios, without using method overloading:
// No arguments
var result = GetValue("Bob", () => { return "Bob Smith"; });
// 1 argument
var result = GetValue("Joe", (i) => { return "Joe " + i.ToString(); });
// 2 arguments
var result = GetValue("Henry", (i,e) => {
return $"i: {i.ToString()}, e: {e.ToString()}";
});
Beyond 2 arguments is not necessary right now.. but may be in the future. The calling syntax is the most important bit to me. I'd prefer to not have the caller cast anything.
I've taken a look at this question and the answers but they all seem to require some calling syntax that I would prefer not to use.
Any ideas how this can be accomplished?
.Each
method. I want the caller to be able to specify whether they are interested in none of the args, 1 of the args, or both. I suppose I could just require a 2-arg signature.. but that would not do exactly what I was hoping for :) – SkiGetValue
. – Skiint
, and the second will always be aSystem.Windows.Forms.Control
. – Ski