C# 创建动态类型集合和数组

C# 创建动态类型集合和数组,c#,.net,list,sorting,interface,C#,.net,List,Sorting,Interface,假设我有一个传递给我的方法的接口: public void AlphaToChar(iList _blah) { } 在IList之外,我想提取它的成员类型,并使用它的类型在方法中创建其他数组或列表。见下面的例子 “List=newlist();”部分不起作用,因为我假设它是一个类型变量,而不是实际的类型。 有办法吗?我如何才能完成这项工作并创建一个提取类型的新集合 Type[] listTypes = list.GetType().GetGenericArguments(); Type

假设我有一个传递给我的方法的接口:

public void AlphaToChar(iList _blah)
{

}
在IList之外,我想提取它的成员类型,并使用它的类型在方法中创建其他数组或列表。见下面的例子

“List=newlist();”部分不起作用,因为我假设它是一个类型变量,而不是实际的类型。 有办法吗?我如何才能完成这项工作并创建一个提取类型的新集合

Type[] listTypes =  list.GetType().GetGenericArguments();
Type listType = null;
if (listTypes.Length>0)
{
   listType = listTypes[0];
}
List<listType> = new List<listType>();
Type[]listTypes=list.GetType().GetGenericArguments();
类型listType=null;
如果(listTypes.Length>0)
{
listType=listTypes[0];
}
列表=新列表();

谢谢。

这将为指定的元素类型动态构造一个通用的
列表

IList list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
IList list=(IList)Activator.CreateInstance(typeof(list).MakeGenericType(elementType));

请注意,结果变量不是静态类型化到专用列表的,因为您在编译时不知道类型。因此,它不可能是静态类型的。您正在利用
List
也在此处实现
IList
这一事实。

这将动态地为指定的元素类型构造一个通用
List

IList list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
IList list=(IList)Activator.CreateInstance(typeof(list).MakeGenericType(elementType));
请注意,结果变量不是静态类型化到专用列表的,因为您在编译时不知道类型。因此,它不可能是静态类型的。您正在利用
List
也在此处实现
IList
这一事实。

您可以使用以下方法执行
List
构造:

// Find the generic argument type of your old list (theList)
Type genericType = theList.GetType().GetGenericArguments()[0];

// Create a new List with the same generic type as the old one
Type newListType = typeof(List<>).MakeGenericType(genericType);

// Create a new instance of the list with the generic type
var instance = Activator.CreateInstance(newListType);
或者让它更通用:

public void AlphaToChar<T>(IList<T> _blah) /* where T : ISomething, new() */ {}  
public void AlphaToChar(IList_blah)/*其中T:ISomething,new()*/{}
如果不这样做,您应该知道IList将包含哪些内容,而不必使用反射来确定其元素类型。

您可以使用以下方法进行列表的构建:

// Find the generic argument type of your old list (theList)
Type genericType = theList.GetType().GetGenericArguments()[0];

// Create a new List with the same generic type as the old one
Type newListType = typeof(List<>).MakeGenericType(genericType);

// Create a new instance of the list with the generic type
var instance = Activator.CreateInstance(newListType);
System.Collections.IList list = 
Activator.CreateInstance(typeof(List<>)
.MakeGenericType(listTypes[0])) as System.Collections.IList;
或者让它更通用:

public void AlphaToChar<T>(IList<T> _blah) /* where T : ISomething, new() */ {}  
public void AlphaToChar(IList_blah)/*其中T:ISomething,new()*/{}
如果不这样做,您应该知道您的
IList
将包含什么,并且不必使用反射来确定其元素类型。

System.Collections.IList list=
System.Collections.IList list = 
Activator.CreateInstance(typeof(List<>)
.MakeGenericType(listTypes[0])) as System.Collections.IList;
Activator.CreateInstance(类型(列表) .MakeGenericType(listTypes[0])作为System.Collections.IList;
System.Collections.IList列表=
Activator.CreateInstance(类型(列表)
.MakeGenericType(listTypes[0])作为System.Collections.IList;

这就是问题所在。我不知道我的IList是什么类型的,这就是为什么我需要找到它。有了实例变量后,如何将其作为列表类型使用,以便对其进行排序(例如Sort())?谢谢你的帮助help@Sev,请记住,
List
以外的其他类可以实现
IList
,并且不要求实现
IList
的集合专用于特定类型。实现者可以选择只允许一个特定类型(如果违反了这个条件,就会抛出异常),但也可以自由接受任何类型,甚至可以使用更复杂的规则来决定允许什么。这就是问题所在。我不知道我的IList是什么类型的,这就是为什么我需要找到它。有了实例变量后,如何将其作为列表类型使用,以便对其进行排序(例如Sort())?谢谢你的帮助help@Sev,请记住,
List
以外的其他类可以实现
IList
,并且不要求实现
IList
的集合专用于特定类型。实现者可以选择只允许某个特定类型(如果违反了该选项,则抛出异常),但也可以自由接受任何类型,甚至可以使用更复杂的规则来决定允许什么。