[英]Variable number of method parameters with different types for each of them
Edit: 编辑:
func(params dynamic[] parameters)
{
}
lets it accept variable parameters with variable types. 让它接受具有可变类型的可变参数。 Ignorance is not bliss.
无知不是幸福。
Question: 题:
I need to write a method that takes n number of lists each of different types such as: 我需要编写一个方法,该方法需要n个不同类型的列表,例如:
List<Type1> list1 = .....;
List<Type2> list2 = .....;
List<TypeN_1> listN_1 = .....;
List<TypeN> listN = .....;
var result=func(list1,list2,listN);
but I couldn't manage with "params" keyword because it doesn't let inner functions know < T > of each list. 但是我无法使用“ 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;
}
Do you truly need such a function? 您真的需要这样的功能吗? Perhaps you can instead write a function which works on two lists at a time, producing a new list.
也许您可以改为编写同时在两个列表上工作的函数,从而生成一个新列表。
List<C> Combine<A,B,C>(List<A>, List<B>, Func<A,B,C>)
Then you can handle multiple lists. 然后,您可以处理多个列表。
Combine(Combine(Combine(a, b, f1), c, f2), d, f3);
Combine(a, Combine(b, Combine(c, d, f1), f2), f3);
Without more context I cannot say whether this is possible for your problem. 没有更多的上下文,我不能说这是否可能解决您的问题。
The greatest common denominator of lists with different generic type parameters is IList
具有不同泛型类型参数的列表的最大公分母是
IList
int func(params IList[] parameters)
{
}
You can then get the generic type parameter with 然后,您可以使用
Type t = parameters[i].GetType();
if (t.IsGenericType)
{
Type typeArgument = t.GetGenericArguments()[0];
...
See: Type.GetGenericArguments Method () 请参阅: Type.GetGenericArguments方法()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.