编辑:
func(params dynamic[] parameters)
{
}
让它接受具有变量类型的变量参数。无知不是幸福。
问题:
我需要编写一个方法,该方法需要n个不同类型的列表,例如:
List<Type1> list1 = .....;
List<Type2> list2 = .....;
List<TypeN_1> listN_1 = .....;
List<TypeN> listN = .....;
var result=func(list1,list2,listN);
但是我无法使用“ params”关键字进行管理,因为它无法让内部函数知道每个列表的<T>。
public int func< ? ? >(? ? ? ?)
{
int result=0;
... get all lists and put them in some function:
innerFunc(list1);
// which is declared as innerFunc<T>(List<T> p){}
return result;
}
具有不同泛型类型参数的列表的最大公分母是 IList
int func(params IList[] parameters)
{
}
然后,您可以使用
Type t = parameters[i].GetType();
if (t.IsGenericType)
{
Type typeArgument = t.GetGenericArguments()[0];
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句