using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Rs.Framework { public class EntityHelper { /// /// 实体类转换类 /// /// /// 需要转换的Dr /// public static T ToEntity(DataRow dr) where T: new() { T entity = new T(); PropertyInfo[] pis = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (PropertyInfo propertyInfo in pis) { if(dr.Table.Columns.Contains(propertyInfo.Name)) { if (propertyInfo.PropertyType.IsEnum) { propertyInfo.SetValue(entity, Enum.Parse(propertyInfo.PropertyType, dr[propertyInfo.Name].ToString())); } else { propertyInfo.SetValue(entity, Convert.ChangeType(dr[propertyInfo.Name], propertyInfo.PropertyType)); } } } return entity; } } }