传递一个泛型List<>在C#中 [英] Passing a generic List<> in C#
问题描述
我会脑死亡这个;我有几个列表的定义,根据特定类(C1,C2,C3 ...)。我将处理这些邮件列表信息的方法。我想要做的是通过在具体名单,但该方法接受泛型列表,然后通过typeof运算决定做什么具体工作。我知道它可能的,但我不能似乎得到的方法侧的语法正确。因此,例如:
I am going brain dead on this; I have several List' defined, based on specific classes (c1, c2, c3...). I have a method that will process information on these lists. What I want to do is pass in the specific list, but have the method accept the generic list, and then via typeof determine what specific work to do. I know its possible, but I cant seem to get the syntax right on the method side. so, for example:
List<c1> c1var;
List<c2> c2var;
List<c3> c3var;
some_method(c1var);
some_method(c2var);
some_method(c3var);
class some_thing
some_method(List<> somevar)
if typeof(somevar).name = x then
esle if typeof(somevar).name = y then....
我如何设置该方法的参数列表?
How do I set up the parameter list for the method?
感谢
推荐答案
您需要声明 some_method
是通用,以及
You need to declare some_method
to be generic, as well.
void SomeMethod<T>(List<T> someList)
{
if (typeof(T) == typeof(c1))
{
// etc
}
}
这篇关于传递一个泛型List<>在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!