目前我正在这样做(//编辑:这完全是愚蠢的,因为有一个数组以与我昨天做我的字典相同的方式对其项目进行编号):
Dictionary<int, Type> genericMap = new Dictionary<int, Type>
{
{ 0, typeof(Action) },
{ 1, typeof(Action<>) },
{ 2, typeof(Action<,>) },
{ 3, typeof(Action<,,>) },
{ 4, typeof(Action<,,,>) },
{ 5, typeof(Action<,,,,>) },
{ 6, typeof(Action<,,,,,>) },
{ 7, typeof(Action<,,,,,,>) },
{ 8, typeof(Action<,,,,,,,>) },
};
还有别的地方...
var parms = meth.GetParameters();
dType = genericMap[parms.Length].MakeGenericType(parms.Select(p => p.ParameterType).ToArray());
其中 meth 是 MethodInfo。
有没有更优雅的方法来做到这一点?或者我是否必须定义这样的映射才能动态获取与参数计数相对应的正确 Action<> 类型?