C# 转换列表<;T>;到datatable,包括T.CustomClass属性
如下所示,我已成功地将C# 转换列表<;T>;到datatable,包括T.CustomClass属性,c#,generics,datatable,system.reflection,C#,Generics,Datatable,System.reflection,如下所示,我已成功地将列表转换为数据表,但还有更多内容。我的T对象基本上是一个具有属性的自定义类,还引用了另一个类。现在我还需要将该类的属性添加到datatable中 这就是函数 public static DataTable ToDataTable<T>(this IList<T> list) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
列表
转换为数据表
,但还有更多内容。我的T对象
基本上是一个具有属性的自定义类,还引用了另一个类。现在我还需要将该类的属性添加到datatable中
这就是函数
public static DataTable ToDataTable<T>(this IList<T> list)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < properties.Count; i++)
{
PropertyDescriptor prop = properties[i];
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
object[] values = new object[properties.Count];
foreach (T item in list)
{
for (int i = 0; i < values.Length; i++)
values[i] = properties[i].GetValue(item) ?? DBNull.Value;
table.Rows.Add(values);
}
return table;
}
我正在开发一个只在DataTables上工作的自定义网格控件,因此此功能对我来说至关重要。由于有许多网格需要自定义,我需要此功能,我无法手动创建每个
DataTable
。但是,您的功能通常是正确的,反射返回的属性描述符与它们来自的类型相关,因此propList[i].GetValue(item)
希望item是子类型。i、 e MyParent.MyChild.Description的属性描述符要求项的类型为MyChild,而不是MyParent
我对您的代码做了一些更改,以便它保存一个属性描述符数组,并依次使用它们来检索中间属性的值
此代码不处理空值。您可能应该修改GetValueFromProps以正确处理空值
private static DataTable AddColumnsForProperties(string myNamespace, DataTable dt, List<PropertyDescriptor> p, ref List<PropertyDescriptor[]> properties)
{
var pLast = p.Last();
if (pLast.PropertyType.Namespace.StartsWith(myNamespace))
{
var allProperties = pLast.GetChildProperties();
foreach (PropertyDescriptor item in allProperties)
{
var newP = p.ToList();
newP.Add(item);
if (item.PropertyType.Namespace.ToLower().StartsWith(myNamespace))
AddColumnsForProperties(myNamespace, dt, newP, ref properties);
else
if (!dt.Columns.Contains(item.Name))
{
dt.Columns.Add(item.Name, Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType);
properties.Add(newP.ToArray());
}
}
}
else
if (!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType);
properties.Add(p.ToArray());
}
return dt;
}
static object GetValueFromProps(PropertyDescriptor[] descriptors, object item)
{
var result = item;
foreach (var descriptor in descriptors)
{
result = descriptor.GetValue(result);
}
return result;
}
public static DataTable ToDataTable<T>(this IList<T> list)
{
DataTable table = null;
if (list != null && list.Count > 0)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
List<PropertyDescriptor[]> propList = new List<PropertyDescriptor[]>();
table = new DataTable();
foreach (PropertyDescriptor item in properties)
{
AddColumnsForProperties(typeof(T).Namespace, table, (new[] { item }).ToList(), ref propList);
}
object[] values = new object[propList.Count];
foreach (T item in list)
{
for (int i = 0; i < values.Length; i++)
values[i] = GetValueFromProps(propList[i], item) ?? DBNull.Value;
table.Rows.Add(values);
}
}
return table;
}
私有静态数据表AddColumnsForProperties(字符串myNamespace、数据表dt、列表p、引用列表属性)
{
var pLast=p.Last();
if(pLast.PropertyType.Namespace.StartsWith(myNamespace))
{
var allProperties=pLast.GetChildProperties();
foreach(allProperties中的PropertyDescriptor项)
{
var newP=p.ToList();
新增(项目);
if(item.PropertyType.Namespace.ToLower().StartsWith(myNamespace))
AddColumnsForProperties(myNamespace、dt、newP、ref属性);
其他的
如果(!dt.Columns.Contains(item.Name))
{
dt.Columns.Add(item.Name,null.GetUnderlyingType(item.PropertyType)??item.PropertyType);
Add(newP.ToArray());
}
}
}
其他的
如果(!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name,null.GetUnderlyingType(pLast.PropertyType)??pLast.PropertyType);
添加(p.ToArray());
}
返回dt;
}
静态对象GetValueFromProps(PropertyDescriptor[]描述符,对象项)
{
var结果=项目;
foreach(描述符中的var描述符)
{
结果=描述符.GetValue(结果);
}
返回结果;
}
公共静态数据表ToDataTable(此IList列表)
{
DataTable=null;
if(list!=null&&list.Count>0)
{
PropertyDescriptorCollection属性=TypeDescriptor.GetProperties(typeof(T));
List propList=新列表();
table=新数据表();
foreach(属性中的PropertyDescriptor项)
{
AddColumnsForProperties(typeof(T).名称空间,表,(new[]{item}).ToList(),ref-propList);
}
object[]value=新对象[propList.Count];
foreach(列表中的T项)
{
for(int i=0;i
我自定义了这个函数,例如,相同名称空间的枚举可以工作,并且子元素具有父元素的前缀。
例如,如果一个类Person有一个元素car,该元素具有类似CarModell的属性,那么列标题将是:“Person.car.CarModell”
private static void AddColumnsForProperties(string myNamespace, string parentName, DataTable dt, List<PropertyDescriptor> p, ref List<PropertyDescriptor[]> properties)
{
var pLast = p.Last();
if (pLast.PropertyType.Namespace != null && pLast.PropertyType.Namespace.StartsWith(myNamespace))
{
var allProperties = pLast.GetChildProperties();
if (allProperties.Count > 0)
{
foreach (PropertyDescriptor item in allProperties)
{
var newP = p.ToList();
newP.Add(item);
string childParentName = !String.IsNullOrEmpty(parentName)
? String.Format("{0}.{1}.{2}", parentName, pLast.Name, item.Name)
: String.Format("{0}.{1}", pLast.Name, item.Name);
if (item.PropertyType.Namespace != null && item.PropertyType.Namespace.ToLower().StartsWith(myNamespace))
{
AddColumnsForProperties(myNamespace, childParentName, dt, newP, ref properties);
}
else if (!dt.Columns.Contains(childParentName))
{
dt.Columns.Add(childParentName, Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType);
properties.Add(newP.ToArray());
}
}
}
else if (!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType);
properties.Add(p.ToArray());
}
}
else if (!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType);
properties.Add(p.ToArray());
}
}
public static DataTable ToDataTable<T>(this IList<T> list)
{
DataTable table = null;
if (list != null && list.Count > 0)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
List<PropertyDescriptor[]> propList = new List<PropertyDescriptor[]>();
table = new DataTable();
foreach (PropertyDescriptor item in properties)
{
// Arrays oder Collections werden nicht in die DataTable eingefügt, da dies eher eine Art Master-Detail-Table ist.
if (IsArrayOrCollection(item.PropertyType))
{
continue;
}
AddColumnsForProperties(typeof(T).Namespace, null, table, (new[] { item }).ToList(), ref propList);
}
object[] values = new object[propList.Count];
foreach (T item in list)
{
for (int i = 0; i < values.Length; i++)
values[i] = GetValueFromProps(propList[i], item) ?? DBNull.Value;
table.Rows.Add(values);
}
}
return table;
}
private static void AddColumnsForProperties(字符串myNamespace、字符串parentName、数据表dt、列表p、引用列表属性)
{
var pLast=p.Last();
if(pLast.PropertyType.Namespace!=null&&pLast.PropertyType.Namespace.StartsWith(myNamespace))
{
var allProperties=pLast.GetChildProperties();
如果(allProperties.Count>0)
{
foreach(allProperties中的PropertyDescriptor项)
{
var newP=p.ToList();
新增(项目);
string childParentName=!string.IsNullOrEmpty(parentName)
?String.Format(“{0}.{1}.{2}”,parentName,pLast.Name,item.Name)
:String.Format(“{0}.{1}”,pLast.Name,item.Name);
if(item.PropertyType.Namespace!=null&&item.PropertyType.Namespace.ToLower().StartsWith(myNamespace))
{
AddColumnsForProperties(myNamespace、childParentName、dt、newP、ref属性);
}
如果(!dt.Columns.Contains(childParentName))为else
{
添加(childParentName,Nullable.GetUnderlyingType(item.PropertyType)??item.PropertyType);
Add(newP.ToArray());
}
}
}
else如果(!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name,null.GetUnderlyingType(pLast.PropertyType)??pLast.PropertyType);
添加(p.ToArray());
}
}
else如果(!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name,null.GetUnderlyingType(pLast.PropertyType)??pLast.PropertyType);
添加(p.ToArray());
}
}
公共静态数据表ToDataTable(此IList列表)
{
DataTable=null;
if(list!=null&&list.Count>0)
{
PropertyDescriptorCollection属性=TypeDescriptor.GetProperties(typeof(T));
List propList=新列表();
table=新数据表();
foreach(属性中的PropertyDescriptor项)
{
//集合数组在模具数据表eingefügt中不存在,在艺术主明细表ist中不存在。
if(IsArrayOrCollection(item.PropertyType))
{
继续;
}
AddColumnsForProperties(典型
private static void AddColumnsForProperties(string myNamespace, string parentName, DataTable dt, List<PropertyDescriptor> p, ref List<PropertyDescriptor[]> properties)
{
var pLast = p.Last();
if (pLast.PropertyType.Namespace != null && pLast.PropertyType.Namespace.StartsWith(myNamespace))
{
var allProperties = pLast.GetChildProperties();
if (allProperties.Count > 0)
{
foreach (PropertyDescriptor item in allProperties)
{
var newP = p.ToList();
newP.Add(item);
string childParentName = !String.IsNullOrEmpty(parentName)
? String.Format("{0}.{1}.{2}", parentName, pLast.Name, item.Name)
: String.Format("{0}.{1}", pLast.Name, item.Name);
if (item.PropertyType.Namespace != null && item.PropertyType.Namespace.ToLower().StartsWith(myNamespace))
{
AddColumnsForProperties(myNamespace, childParentName, dt, newP, ref properties);
}
else if (!dt.Columns.Contains(childParentName))
{
dt.Columns.Add(childParentName, Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType);
properties.Add(newP.ToArray());
}
}
}
else if (!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType);
properties.Add(p.ToArray());
}
}
else if (!dt.Columns.Contains(pLast.Name))
{
dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType);
properties.Add(p.ToArray());
}
}
public static DataTable ToDataTable<T>(this IList<T> list)
{
DataTable table = null;
if (list != null && list.Count > 0)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
List<PropertyDescriptor[]> propList = new List<PropertyDescriptor[]>();
table = new DataTable();
foreach (PropertyDescriptor item in properties)
{
// Arrays oder Collections werden nicht in die DataTable eingefügt, da dies eher eine Art Master-Detail-Table ist.
if (IsArrayOrCollection(item.PropertyType))
{
continue;
}
AddColumnsForProperties(typeof(T).Namespace, null, table, (new[] { item }).ToList(), ref propList);
}
object[] values = new object[propList.Count];
foreach (T item in list)
{
for (int i = 0; i < values.Length; i++)
values[i] = GetValueFromProps(propList[i], item) ?? DBNull.Value;
table.Rows.Add(values);
}
}
return table;
}