下面的代碼段對於給定的方法工作得很好,但我希望在類構造期間執行相同的操作。怎麼做?如何將未知類型的通用列表傳遞給類構造函數
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