如何将类列表作为函数参数c#传递 [英] How to pass class list as function parameter c#
本文介绍了如何将类列表作为函数参数c#传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有一个像bellow这样的功能。
Hi I have a function like bellow.
public void test(??)
{
..........
..........
/*Here I nedd call list ,which I send as parametar*/
List<??> rangePlants = something;
..........
..........
}
我试图发送我的应用程序的任何类的列表作为parameter.suppose我有3个类A,B,C有时我必须传递List ,List 或List< c> .can any身体帮我一个例子?
推荐答案
你可以在这里使用Interface。
创建一个接口说我并在所有3个类A,B,C中实现该接口。
之后在测试方法中你可以接受我和在列表中你也可以使用我,比如..
You can make use of Interface over here.
Create an interface say "I" and implement that interface in all the 3 classes A,B,C.
After that in the method Test you can accept "I" and in the list also you can use "I", like..
public void test(I interfaceImplementedObject)
{
..........
..........
/*Here I nedd call list ,which I send as parametar*/
List<I> rangePlants = something;
..........
..........
}
你最好把它变成通用的:
You better make it generic:
public void test<t>(rest of the parameter list)
{
..........
..........
List<t> rangePlants = something;
..........
..........
}
</t></t>
有可能在不适用通用方法的情况下,例如 Activator [ ^ ],如您所见,您可以在链接上看到输入
类型参数。
There might be situations where generic approach this is not applicable, like in case of Activator[^], in tat case, as you can see on the link you can pass Type
typed parameter.
这篇关于如何将类列表作为函数参数c#传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文