How make Dictionary with lambda functions, with different numbers of parameters [closed]

不打扰是莪最后的温柔 提交于 2019-12-13 22:13:49

问题


How make Dictionary with lambda functions, that Func<> contains different numbers of parameters?

Dictionary<string, Func<T, double>> operationsList 
  = new Dictionary<string, Func<T, double>>();

It mast be so?


回答1:


You can make your dictionary type Delegate, but I'm pretty sure that calling Func<> stored this way will be a pain:

var dict = new Dictionary<int, Delegate>();
dict.Add(1, (Func<int, int>)((int x) => x * 10));
dict.Add(2, (Func<int>)(() => 10));

Calling:

var result1 = dict[1].DynamicInvoke(10);
var result2 = dict[2].DynamicInvoke();


来源:https://stackoverflow.com/questions/21099366/how-make-dictionary-with-lambda-functions-with-different-numbers-of-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!