利用反射快速给Model实体赋值

来源:互联网 发布:excel中数据透视表 编辑:程序博客网 时间:2024/04/29 10:04
        public class BaseModel<T> where T : new()        {            public static T Init(DataRow dr)            {                T t = new T();                Type infotype = typeof(T);                //获取所有属性                PropertyInfo[] propertys = infotype.GetProperties();                //遍历属性并将datarow中字段的值赋值给同名属性                foreach (PropertyInfo pi in propertys)                {                  infotype.GetProperty(pi.Name).SetValue(t, result, null);                }                return t;            }        }                BaseModel<Student>.Init(dr)