public static T FillModel<T>(DataRow dr) { if (dr == null || dr.ItemArray.Length == 0) return default(T); T model = Activator.CreateInstance<T>(); foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))//BindingFlags.IgnoreCase 忽略大小写,BindingFlags.NonPublic 私有属性 { if (dr[property.Name] != null) { property.SetValue(model, Convert.ChangeType(dr[property.Name], property.PropertyType), null); } } return model; }
调用:
var yhxx = ModelReflecter.FillModel<YhxxEntity>(dt);