[英]C# Passing a generic list into method and then casting to type
I have a method that uses a list which I need to pass into it. 我有一个方法,使用我需要传递给它的列表。
Depending on where I call this method the type of the list is different. 根据我调用此方法的位置,列表的类型不同。
Is it possible to pass the list in as a generic list and then cast it to a certain type, or do I need to create parameters for every type of list that can be used (see below)? 是否可以将列表作为通用列表传递,然后将其转换为某种类型,或者我是否需要为可以使用的每种类型的列表创建参数(见下文)?
public string MyMethod(List<Type1> list1, List<Type2> list2, ...etc )
{
switch(someCondition)
{
case 1:
//pass list1 into a method from class1
break;
case 2:
//pass list2 into a method from class2
break;
...//etc for several more lists
}
Instead of making a single method with multiple parameters, why don't you make several methods with a single List type? 而不是使用多个参数制作单个方法,为什么不使用单个List类型创建多个方法? It seems you are not sharing much code from one List type to another anyway.
无论如何,您似乎没有从一个List类型到另一个类型共享很多代码。
public string MyMethod(List<int> list)
{
//do something with list
}
public string MyMethod(List<bool> list)
{
//do something with list1
}
...
you can create a generic function that accepts generic type like this 你可以创建一个接受这样的泛型类型的泛型函数
public virtual List<T> SelectRecord <T>(int items, IEnumerable<T> list)
{
if (items == -1)
return list.ToList<T>();
else
if (items > list.Count<T>())
return list.ToList<T>();
else
return list.Take<T>(items).ToList<T>();
}
In my example I passed a IEnumerable to my function and it returns a List (where is the type of my object). 在我的例子中,我将IEnumerable传递给我的函数,它返回一个List(我的对象的类型)。 I don't specify what is the type of the object.
我没有指定对象的类型。
I hope it's helpful. 我希望它有用。
You could do something like this: 你可以这样做:
class GenericsExample1Class<T>
{
public string WhatsTheType(List<T> passedList)
{
string passedType = string.Empty;
passedType = passedList.GetType().ToString();
return passedType;
}
}
private void ClienMethod()
{
string typeOfTheList = string.Empty;
// Call Type 1: Value Type
List<int> intergerObjectList = new List<int> { 1, 2 };
typeOfTheList = (new GenericsExample1Class<int>()).WhatsTheType(intergerObjectList);
MessageBox.Show(typeOfTheList);
// Call Type 2: Reference Type
List<string> stringObjectList = new List<string> { "a", "b" };
typeOfTheList = (new GenericsExample1Class<string>()).WhatsTheType(stringObjectList);
MessageBox.Show(typeOfTheList);
// Call Type 2: Complex-Reference Type
List<Employee> complexObjectList = new List<Employee> { (new Employee { Id = 1, Name = "Tathagat" }), (new Employee { Id = 2, Name = "Einstein" }) };
typeOfTheList = (new GenericsExample1Class<Employee>()).WhatsTheType(complexObjectList);
MessageBox.Show(typeOfTheList);
}
Thanks for all the answers. 感谢所有的答案。
Another solution I just discovered would be to use a new feature in C# 4.0: 我刚刚发现的另一个解决方案是在C#4.0中使用新功能:
public string MyMethod(List<Type1> list1 = null, List<Type2> list2 = null, ...etc )
{
switch(someCondition)
{
case 1:
//pass list1 into a method from class1
break;
case 2:
//pass list2 into a method from class2
break;
...//etc for several more lists
}
}
Then I could just call the method like so and not have to specify all the parameters: 然后我可以像这样调用方法而不必指定所有参数:
MyMethod(list1: mylist1)
Yes, this can be done easly by passing parameter with Object type, as shown below: 是的,这可以通过使用Object类型传递参数来轻松完成,如下所示:
public void Test(int SomeCondition,Object Param)
{
dynamic list;
switch (SomeCondition)
{
case 0:
list = (List<int>)Param;
MessageBox.Show(list[0].ToString());
break;
case 1:
list = (List<string>)Param;
MessageBox.Show(list[0].ToString());
break;
default:
MessageBox.Show("Default!");
break;
}
}
You can call this function this way: 你可以这样调用这个函数:
List<int> list1 = new List<int>(new int[]{1,2,3});
List<string> list2 = new List<string>(new string[] { "one", "two", "three"});
Test(0, list1);
Test(1, list2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.