下面的代码段对于给定的方法工作得很好,但我希望在类构造期间执行相同的操作。怎么做?如何将未知类型的通用列表传递给类构造函数
public static DataTable ToDataTable<T>(IList<T> data)
想要这样的东西...但构造函数不喜欢<T>(IList<T>
部分。
public class DTloader
{
PropertyDescriptorCollection props;
DataTable dataTable = new DataTable();
public DTloader<T>(IList<T> data)
{
props = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
dataTable.Columns.Add(prop.Name, prop.PropertyType);
}
}
.......
这看起来类似于你的问题:https://stackoverflow.com/questions/39196357/generic-class-with-generic-constructor – SchwiftyPython