如何使用具有不同数量参数的lambda函数制作Dictionary [英] How make Dictionary with lambda functions, with different numbers of parameters

查看:162
本文介绍了如何使用具有不同数量参数的lambda函数制作Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Lambda函数使Dictionary包含Func<>包含不同数量的参数?

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>>();

是这样吗?

推荐答案

您可以将字典类型设置为Delegate,但我很确定以这种方式存储的调用Func<>会很麻烦:

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));

通话:

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

这篇关于如何使用具有不同数量参数的lambda函数制作Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆