示例#1
0
        /// <summary>
        /// 取一个实例的所有属性和字段
        /// </summary>
        /// <param name="obj">对象实例</param>
        /// <returns>属性数组</returns>
        public static PropInfo[] GetProps(object obj)
        {
            BindingFlags    flags = BindingFlags.Public | BindingFlags.Instance;
            Type            type  = obj.GetType();
            List <PropInfo> list  = new List <PropInfo>();

            foreach (PropertyInfo propertyInfo in type.GetProperties(flags))
            {
                list.Add(new PropInfo(obj, (PropertyInfo)propertyInfo));
            }
            foreach (FieldInfo fieldInfo in type.GetFields(flags))
            {
                list.Add(new PropInfo(obj, (FieldInfo)fieldInfo));
            }
            PropInfo[] answer = new PropInfo[list.Count];
            list.CopyTo(answer);
            return(answer);
        }
示例#2
0
        /// <summary>
        /// 设属性值,属性不存在则抛出异常
        /// </summary>
        public static void SetPropValue(object obj, string name, object value)
        {
            PropInfo prop = GetProp(obj, name);

            prop.Value = value;
        }
示例#3
0
        /// <summary>
        /// 取属性值,属性不存在则抛出异常
        /// </summary>
        public static object GetPropValue(object obj, string name)
        {
            PropInfo prop = GetProp(obj, name);

            return(prop.Value);
        }