C# 泛型列表作为构造函数参数
C# generic list as constructor parameter
我正在尝试将我不知道类型的列表提供给 class 的构造函数。
首先我有一个 class,其中包含多个表和一个方法:
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));
}
}
}
}
我正在努力实现的通用 class:
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 捕获的任何泛型列表的)传递给泛型构造函数?
在此先感谢您的帮助
像这样..
if(property.GetValue(this,null) is IList
&& property.GetValue(this,null).GetType().IsGenericType){
var listType = property.PropertyType.GetGenericArguments()[0];
var confType = typeof(ConfEntryList<>).MakeGenericType(listType);
var item = (ConfEntry)Activator.CreateInstance(confType,
new object [] {property.Name, property.GetValue(this, null)});
confContainer.ConfEntries [i] = item;
}
我正在尝试将我不知道类型的列表提供给 class 的构造函数。
首先我有一个 class,其中包含多个表和一个方法:
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));
}
}
}
}
我正在努力实现的通用 class:
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 捕获的任何泛型列表的)传递给泛型构造函数?
在此先感谢您的帮助
像这样..
if(property.GetValue(this,null) is IList
&& property.GetValue(this,null).GetType().IsGenericType){
var listType = property.PropertyType.GetGenericArguments()[0];
var confType = typeof(ConfEntryList<>).MakeGenericType(listType);
var item = (ConfEntry)Activator.CreateInstance(confType,
new object [] {property.Name, property.GetValue(this, null)});
confContainer.ConfEntries [i] = item;
}