通过反射给对象属性进行赋值 | 天南一隅

通过反射给对象属性进行赋值

时间:2010-11-29  |   0  |  

看到公司项目中用到了反射于是闲的无聊写了个动态给子类赋值的方法。
基类:BaseObject

用于获取子类类型的方法


        public static BaseObject GetTypeByName(string typeName)
        {
            switch (typeName.ToLower())
            {
                case "user":
                    return new User();
                
                cass "基类"
                    return new 基类();

                cass ..........
                default:
                    return (BaseObject)null;
            }
        }

用于获取绑定属性的方法

  //keyAndValue 存放的为属性名称与值 来自与数据库 对应数据库的字段与字段值
  public static BaseObject GetObjByString(string tableName, Dictionary keyAndValue)
        {
            BaseObject Obj = GetTypeByName(tableName);//使用上面的方法获取对象
            if (Obj == null)
            {
                return Obj;
            }
            Type t = Obj.GetObjectType();//基类的一个返回当前类型的方法,子类继承后重写
            PropertyInfo[] fields = t.GetProperties();
            string tmpStr = null;
            foreach (PropertyInfo field in fields)
            {
                tmpStr = keyAndValue[field.Name];
                if (string.IsNullOrEmpty(tmpStr))
                {
                    field.SetValue(Obj, string.Empty, null);
                }
                else
                {
                    field.SetValue(Obj, getValue(field.PropertyType, tmpStr), null);//getValue方法用于根据属性类型对值进行相应的转化然后赋给属性
                }
            }
            return Obj;
         }

动态转化类型方法

        private static Object getValue(Type type, string value)
        {
            return TypeDescriptor.GetConverter(type).ConvertFrom(value);
        }

User类实体

 public class User : BaseObject
    {
        public User() { }
        public User(int ID) : base(ID) { }

        private int _id;
        private string _username = "";
        private string _password = "";
        private string _name = "";
        private int _city_id = 0;
        private int _department_id = 0;
        private int _userlevel = 0;
        private int _state = 1;
        private string _authorities = "";
        private string _menulist = "";
        private DateTime _lastactivity = DateTime.Now;
        private DateTime _ctime = DateTime.Now;
        private decimal _phcardprofit;
        private int _UserType;



        public int UserType
        {
            get { return _UserType; }
            set { _UserType = value; }
        }


        public decimal PhCardProfit
        {
            set { _phcardprofit = value; }
            get { return _phcardprofit; }
        }


        /// 
        /// 返回对象类型
        /// 
        public override Type GetObjectType()
        {
            return Type.GetType("NewspaperMis.Model.User");
        }

        /// 
        /// 
        /// 
        public override int id
        {
            set { _id = value; }
            get { return _id; }
        }

        /// 
        /// 
        /// 
        public string UserName
        {
            set { _username = value; }
            get { return _username; }
        }

        /// 
        /// 
        /// 
        public string Password
        {
            set { _password = value; }
            get { return _password; }
        }

        /// 
        /// 名称
        /// 
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }

        /// 
        /// 城市
        /// 
        public int City_ID
        {
            set { _city_id = value; }
            get { return _city_id; }
        }

        /// 
        /// 部门
        /// 
        public int Department_ID
        {
            set { _department_id = value; }
            get { return _department_id; }
        }

        /// 
        /// 
        /// 
        public int UserLevel
        {
            set { _userlevel = value; }
            get { return _userlevel; }
        }

        /// 
        /// 状态
        /// 
        public int State
        {
            set { _state = value; }
            get { return _state; }
        }

        /// 
        /// 权限
        /// 
        public string Authorities
        {
            set { _authorities = value; }
            get { return _authorities; }
        }

        /// 
        /// 权限树
        /// 
        public string MenuList
        {
            set { _menulist = value; }
            get { return _menulist; }
        }

        /// 
        /// 最后活动时间
        /// 
        public DateTime LastActivity
        {
            set { _lastactivity = value; }
            get { return _lastactivity; }
        }

        /// 
        /// 登记时间
        /// 
        public DateTime CTime
        {
            set { _ctime = value; }
            get { return _ctime; }
        }
    }




无觅相关文章插件,快速提升流量