Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C来自Datatable或DataRow的多对一反射_C#_Reflection_Properties_Datarow_Propertyinfo - Fatal编程技术网

C# C来自Datatable或DataRow的多对一反射

C# C来自Datatable或DataRow的多对一反射,c#,reflection,properties,datarow,propertyinfo,C#,Reflection,Properties,Datarow,Propertyinfo,我有一节课 class Person { public string Name {get; set;} public int Age {get; set;} public string Phone {get; set;} // many other properties } 此人的数据来自DataRow,问题是没有一对一的映射,如下所示:- public DataRow doMapping() { DataRow row = new DataRow(); FillDat

我有一节课

class Person {

 public string Name {get; set;}

 public int Age {get; set;}

 public string Phone {get; set;}

 // many other properties

}
此人的数据来自DataRow,问题是没有一对一的映射,如下所示:-

 public DataRow doMapping()
{
 DataRow row = new DataRow();
 FillDataInto(row);
 Person p1 = new Person(); 

 p1.Name = row["FirstName"] + " " + row["LastName"];
 p1.Age = row["Age"];
 p1.Phone = row["Phone"] + " " + row["Extension"];
 p1.Prop1 = row["Prop1"];
 p1.PropXY = row["PropXY"];

 // many others ... 
}

如图所示,一些属性来自数据库的不同列。我需要一些帮助来创建一个反射函数,将行属性映射到Person中

目前我有以下内容,它将用于一对一映射, 如上例中的年龄:-

public DataRow doMapping(){

 DataRow row = new DataRow();
 FillDataInto(row); 

 Person p1 = new Person();
 Type type = Person.GetType();
 PropertyInfo[] props = type.getProperties();

 foreach(PropertyInfo p in props)
 {
   string name = p.Name;
   p.SetValue(p1, row[name]);
 }
}
我只需要改进它来处理前面提到的情况。
我可以稍后清理,一旦我理解了核心逻辑,您就可以在dataTable it self中拥有Name和Phone所需的列

为Name和Phone添加两个新列,并将其定义为与属性所用的逻辑相同。 如果您是从database Select语句加载此dataTable,请在SQL语句本身中为Name和Phone添加两个新列,其逻辑与用于属性的逻辑相同

然后,一对一属性映射的实现可能会起作用