0
我有以下几点:如何使用可选参数调用委托?
var availableDelegates = new Dictionary<string, SampleHandler>{
{"TestWithNoParams", SampleHandlerOne }, // this is what I have now - working
{"TestWithParamSetA", SampleHandlerTwo } // need this
{"TestWithNoParams", SampleHandlerTwo } // and this - working
}
public static SampleHandler SampleHandlerOne(){
// do stuff - working
}
// v1
public static SampleHandler SampleHandlerTwo(){
// do stuff without parameters - working
}
// v2
public static SampleHandler SampleHandlerTwo(HandlerTwoParams params = null) { // trying to update to support optional params
// do stuff with parameters - not working
// params.Foo = bar
}
如何传递可选参数的委托?这个想法是,我可以做:
if (someCondition)
availableDelegates[target].Invoke(optionalParams);
else
availableDelegates[target].Invoke();
SampleHandler似乎没有可选参数。所以这是行不通的。 –
@HansPassant - 这就是为什么代码中有'v1'和'v2'的原因。 – SB2055