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