C# 我可以在DataColumn中存储可序列化的数组数据吗?
我试图将对象的属性自动转换为DataTable(对象是数组,并且具有从具有值类型的特殊类实例化的属性) 守则:C# 我可以在DataColumn中存储可序列化的数组数据吗?,c#,arrays,reflection,datacolumn,C#,Arrays,Reflection,Datacolumn,我试图将对象的属性自动转换为DataTable(对象是数组,并且具有从具有值类型的特殊类实例化的属性) 守则: static public DataTable f_GetDataTableFromClassObject(object _objInstance) { // geri dönecek datatable DataTable dataTable = new DataTable(); // nesnenin propertyleri içinde dolanal
static public DataTable f_GetDataTableFromClassObject(object _objInstance)
{
// geri dönecek datatable
DataTable dataTable = new DataTable();
// nesnenin propertyleri içinde dolanalım ve datatable içine kolon olarak ekleyelim.
foreach (var p in f_GetProperties(_objInstance))
{
if (p.PropertyType.IsArray)
{
if (p.PropertyType.BaseType.Attributes.ToString().IndexOf("Serializable")>-1)
{
// Now i want to store to DataColumn this properties which is instantiated DifferentClass[] and Serializable
}
}
else
{
dataTable.Columns.Add(new DataColumn(p.Name, p.PropertyType));
}
}
// ve tablomuz.
return dataTable;
}
我应该如何将此数组存储在DataColumn中?类程序
class Program
{
static void Main(string[] args)
{
Person cenk = new Person() { adi = "Cenk", yasi = 18 };
List<Person> lst = new List<Person>()
{
cenk,
new Person() {adi = "Cem", yasi = 17, harfler = new[] {"a","b","c"}},
new Person() {adi = "Canan", yasi = 16, harfler = new[] {"a","b","c"}}
};
DataTable dataTable = new DataTable();
PropertyInfo[] pinfo = props();
//var s = pinfo.Select(p => dataTable.Columns.Add(new DataColumn(p.Name, (p.PropertyType.FullName).GetType())));
foreach (var p in pinfo)
{
dataTable.Columns.Add(new DataColumn(p.Name, p.PropertyType));
}
foreach (Person person in lst)
{
DataRow dr = dataTable.NewRow();
foreach (PropertyInfo info in person.GetType().GetProperties())
{
object oo = person.GetType().GetProperty(info.Name).GetValue(person, null);
dr[info.Name] = oo;
}
dataTable.Rows.Add(dr);
}
}
static public PropertyInfo[] props()
{
return (new Person()).GetType().GetProperties();
}
}
public class Person
{
public string adi { get; set; }
public int yasi { get; set; }
public string[] harfler { get; set; }
}
{
静态void Main(字符串[]参数)
{
Person cenk=new Person(){adi=“cenk”,yasi=18};
List lst=新列表()
{
岑克,
newperson(){adi=“Cem”,yasi=17,harfler=new[]{“a”,“b”,“c”},
newperson(){adi=“Canan”,yasi=16,harfler=new[]{“a”,“b”,“c”}
};
DataTable=新的DataTable();
PropertyInfo[]pinfo=props();
//var s=pinfo.Select(p=>dataTable.Columns.Add(newdatacolumn(p.Name,(p.PropertyType.FullName).GetType());
foreach(以pinfo为单位的变量p)
{
添加(新的DataColumn(p.Name,p.PropertyType));
}
foreach(lst中的人员)
{
DataRow dr=dataTable.NewRow();
foreach(PropertyInfo-info-in-person.GetType().GetProperties())
{
对象oo=person.GetType().GetProperty(info.Name).GetValue(person,null);
dr[info.Name]=oo;
}
dataTable.Rows.Add(dr);
}
}
静态公共属性信息[]道具()
{
return(newperson()).GetType().GetProperties();
}
}
公共阶层人士
{
公共字符串adi{get;set;}
公共int yasi{get;set;}
公共字符串[]harfler{get;set;}
}
尝试使用System.Object类型。