假设您有一个字典
Action<string> or Action<int,int,string> etc.
是否有一种通用的方法来发现给定的键 k 指向一个 Action 或 Func 然后调用相同的值?
var value = dict[key];
//some sort of predicate checking if value is invokeable
我考虑过使用 dynamic 关键字并简单地在结果对象上调用 Invoke( args ),但这看起来很慢而且不够优雅。
最佳答案
你可以测试它是一个委托(delegate):
Delegate d = value as Delegate;
if(d != null) d.DynamicInvoke(args);
但是!如果您不知道调用前的签名,那么知道要提供什么参数(在对象数组中)是很棘手的。
关于c# - 在 C# 中确定对象 o 是 Action<...> 还是 Func<....>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7855947/