C# 在c中定义集合时将类名作为字符串#

C# 在c中定义集合时将类名作为字符串#,c#,wpf,C#,Wpf,我将类名作为使用TYPE获得的字符串 Type type = myvar.GetType(); string _className = type.ToString(); // getting the class name 我的问题是如何在下面的代码中使用这个字符串\u className var data = this.ItemsSource as ObservableCollection<**_className**>()[2]; var data=this.itemsou

我将类名作为使用TYPE获得的字符串

Type type = myvar.GetType();  
string _className = type.ToString(); // getting the class name
我的问题是如何在下面的代码中使用这个字符串\u className

var data = this.ItemsSource as ObservableCollection<**_className**>()[2];
var data=this.itemsource as ObservableCollection()[2];
这里ItemsSource是通用的


提前感谢。

您可以使用反射和以下方法完成此操作:

Type Type=myvar.GetType();
string className=type.ToString();
Type genericType=Type.GetType(类名);
类型observableCollectionType=typeof(ObservableCollection);
类型constructedType=observableCollectionType.MakeGenericType(genericType);
var constructedInstance=Activator.CreateInstance(constructedType);

您可以使用反射和以下方法执行此操作:

Type Type=myvar.GetType();
string className=type.ToString();
Type genericType=Type.GetType(类名);
类型observableCollectionType=typeof(ObservableCollection);
类型constructedType=observableCollectionType.MakeGenericType(genericType);
var constructedInstance=Activator.CreateInstance(constructedType);

您可以使用反射和以下方法执行此操作:

Type Type=myvar.GetType();
string className=type.ToString();
Type genericType=Type.GetType(类名);
类型observableCollectionType=typeof(ObservableCollection);
类型constructedType=observableCollectionType.MakeGenericType(genericType);
var constructedInstance=Activator.CreateInstance(constructedType);

您可以使用反射和以下方法执行此操作:

Type Type=myvar.GetType();
string className=type.ToString();
Type genericType=Type.GetType(类名);
类型observableCollectionType=typeof(ObservableCollection);
类型constructedType=observableCollectionType.MakeGenericType(genericType);
var constructedInstance=Activator.CreateInstance(constructedType);

您可以使用
类型的值以及一些反射来创建泛型类型的实例,而不是使用
字符串。但是,您不能在代码中直接使用这两种类型(也就是说,您不能编写
observedcollection
形式的代码,其中
xxx
是一个变量)。可以尝试使用
Type.GetType(\u className)
获取类型。尝试
var data=(this.itemsource as IList)[2]
。然后您不需要知道类型我没有尝试创建泛型类型的实例,我将执行并让我检查,您可以使用
类型的值以及一些反射来创建泛型类型的实例,而不是使用
字符串。但是,您不能在代码中直接使用这两种类型(也就是说,您不能编写
observedcollection
形式的代码,其中
xxx
是一个变量)。可以尝试使用
Type.GetType(\u className)
获取类型。尝试
var data=(this.itemsource as IList)[2]
。然后您不需要知道类型我没有尝试创建泛型类型的实例,我将执行并让我检查,您可以使用
类型的值以及一些反射来创建泛型类型的实例,而不是使用
字符串。但是,您不能在代码中直接使用这两种类型(也就是说,您不能编写
observedcollection
形式的代码,其中
xxx
是一个变量)。可以尝试使用
Type.GetType(\u className)
获取类型。尝试
var data=(this.itemsource as IList)[2]
。然后您不需要知道类型我没有尝试创建泛型类型的实例,我将执行并让我检查,您可以使用
类型的值以及一些反射来创建泛型类型的实例,而不是使用
字符串。但是,您不能在代码中直接使用这两种类型(也就是说,您不能编写
observedcollection
形式的代码,其中
xxx
是一个变量)。可以尝试使用
Type.GetType(\u className)
获取类型。尝试
var data=(this.itemsource as IList)[2]
。那么你不需要知道类型我没有尝试创建泛型类型的实例,我会这样做,让我检查一下
Type type = myvar.GetType();  
string className = type.ToString(); 
Type genericType = Type.GetType(className);
Type observableCollectionType = typeof(ObservableCollection<>);
Type constructedType = observableCollectionType.MakeGenericType(genericType);
var constructedInstance = Activator.CreateInstance(constructedType);