//dtPerson 为表,FName为表里面的字段 string[] personArry = dtPerson.AsEnumerable().Select(d => d.Field<string>("FName")).ToArray();
public static D Mapper<D, S>(S s) { D d = Activator.CreateInstance<D>(); //构造新实例 try { var Types = s.GetType();//获得类型 var Typed = typeof(D); foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段 { foreach (PropertyInfo dp in Typed.GetProperties()) { if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同 { dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性 } } } } catch (Exception ex) { throw ex; } return d; } //使用示例 PTLLocalDB.T_SAP_PurchaseHeaderInfo PTLHeader = new PTLLocalDB.T_SAP_PurchaseHeaderInfo(); PTLHeader = PTLStartSAPFlow.Mapper<PTLLocalDB.T_SAP_PurchaseHeaderInfo, LocalDB.T_SAP_PurchaseHeaderInfo>(header);
原文:https://www.cnblogs.com/gudaozi/p/11618574.html