列为对象获取属性名称 [英] List as object get property names
问题描述
我有一个接受对象作为数据源的类.通常是匿名类型的列表.我想解析此List< t>并使用List< t>中的数据创建一个DataTable.以下是执行此操作的功能.它接受对象数据,该对象数据是List< t>.到目前为止,已经能够获取属性名称来创建列,但是我还没有找到一种获取列值的方法.我相信我必须以某种方式将对象转换为List< t> ;,但还没有弄清楚如何做到这一点.
这是正在进行的功能:
I have a class that accepts an object as a DataSource. Which is usually a List of Anonymous type. I want to parse this List<t> and create a DataTable using the data in the List<t>. Below is the function in progress to doing this. It accepts an object data, which is the List<t> and so far is able to get the property names to create the columns, but I haven''t figured out a way to get the values of the columns. I believe I have to someway cast the object to List<t>, but haven''t figured out how to do that yet.
Here is the function in process:
public System.Data.DataTable ToDataTable(object data)
{
System.Data.DataTable table = new System.Data.DataTable();
System.ComponentModel.PropertyDescriptorCollection props =
System.ComponentModel.TypeDescriptor.GetProperties(
data.GetType().GetGenericArguments()[0]);
for (int i = 0; i < props.Count; i++)
{
System.ComponentModel.PropertyDescriptor prop = props[i];
if (prop.PropertyType.Name.Contains("Nullable"))
table.Columns.Add(prop.Name);
else
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach( var item in data as System.Collections.IEnumerable)
{
for( int i = 0; i < values.Lenght; i++; )
values[i] = props[i].GetValue(item);
table.Rows.Add(values);
}
return (table);
}
推荐答案
如何改用getproperties
how about using getproperties instead
Type mytype = data.GetType();
foreach (var prop in mytype.GetProperties())
{
table.Columns.Add(prop.Name, prop.GetValue(data,null).GetType());
}
公共System.Data.DataTable ToDataTable(对象数据)
{
System.Data.DataTable表=新的System.Data.DataTable();
System.ComponentModel.PropertyDescriptorCollection道具=
System.ComponentModel.TypeDescriptor.GetProperties(
data.GetType().GetGenericArguments()[0]);
为(int i = 0; i< props.Count; i ++)
{
System.ComponentModel.PropertyDescriptor prop = props [i];
如果(prop.PropertyType.Name.Contains("Nullable"))
table.Columns.Add(prop.Name);
其他
table.Columns.Add(prop.Name,prop.PropertyType);
}
object []值=新对象[props.Count];
foreach(数据中的var项作为System.Collections.IEnumerable)
{
for(int i = 0; i< values.Lenght; i ++;)
values [i] = props [i] .GetValue(item);
table.Rows.Add(values);
}
return(table);
}
public System.Data.DataTable ToDataTable(object data)
{
System.Data.DataTable table = new System.Data.DataTable();
System.ComponentModel.PropertyDescriptorCollection props =
System.ComponentModel.TypeDescriptor.GetProperties(
data.GetType().GetGenericArguments()[0]);
for (int i = 0; i < props.Count; i++)
{
System.ComponentModel.PropertyDescriptor prop = props[i];
if (prop.PropertyType.Name.Contains("Nullable"))
table.Columns.Add(prop.Name);
else
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach( var item in data as System.Collections.IEnumerable)
{
for( int i = 0; i < values.Lenght; i++; )
values[i] = props[i].GetValue(item);
table.Rows.Add(values);
}
return (table);
}
这篇关于列为对象获取属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!