我希望能够使用变量数量的匿名方法进行内联调用(有时不带参数,有时带11个参数)。
Dictionary<string, Action> ActionDic = new Dictionary<string, Action>();
int i = 0;
ActionDic["something"] = () => { i += 1; }; // this line is ok
ActionDic["somethingArgs"] = (int n) => { n += 1; }; // but this is not
// Delegate 'System.Action' does not take 1 arguments
所以我不能使委托接受那样的参数。我的语法有错吗,还是根本不可能?或者我必须改变匿名方法的类型来使用字典?
ActionDic
是什么,它的字符串索引器都接受Action
(或另一个没有返回类型的无参委托)。如果它是Action<T>
,你可以这样做:p => DoStuff(p)
。 - Şafak Gür