【发布时间】:2015-03-09 11:56:46
【问题描述】:
我正在尝试将我不知道类型的列表提供给类的构造函数。
首先我有一个包含多个表和一个方法的类:
public class A{
private List<float> _List1;
private List<Proj> _List2;
...
public void saveConfig(string path){
ConfContainer confContainer = new ConfContainer ();
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties();
confContainer.ConfEntries = new ConfEntry[properties.Length];
int i = 0;
foreach (PropertyInfo property in properties){
if(property.GetValue(this,null) is IList && property.GetValue(this,null).GetType().IsGenericType){
confContainer.ConfEntries [i] = new ConfEntryList (property.Name, property.GetValue(this, null));
}
}
}
}
以及我想要实现的泛型类:
public class ConfEntryList<T>: ConfEntry{
[XmlArray("Valeurs")]
[XmlArrayItem("Valeur")]
public List<T> Valeurs;
public ConfEntryList(){
}
public ConfEntryList(string attribut, List<T> valeurs){
this.Attribut = attribut;
this.Valeur = null;
this.Valeurs = valeurs;
}
}
问题是这一行: confContainer.ConfEntries [i] = new ConfEntryList (property.Name, property.GetValue(this, null));
我不知道如何将List类型传递给构造函数:
new ConfEntryList<T>(...)
是否可以将类型 T(PropertyInfo 捕获的任何泛型列表的)传递给泛型构造函数?
提前感谢您的帮助
【问题讨论】:
标签: c# list class generics types