コード例 #1
0
        /// <summary>
        /// 修改值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propName"></param>
        /// <param name="value"></param>
        public static void SetValue(this IPropertyObject obj, String propName, Object value, bool throwException = false)
        {
            if (obj == null)
            {
                return;
            }
            //propName是否是普通成员
            MemberInfo member = obj.GetType().FindMember(propName);

            if (member != null)
            {
                member.SetValue(obj, value);
                return;
            }
            //obj的类型的属性是否已经注册
            PropertyDescriptorCollection pdc = obj.GetPropertyDescriptorCollection();

            if (pdc == null)
            {
                RegisteProperties(obj);
            }
            pdc = obj.GetPropertyDescriptorCollection();
            //没有可以注册的属性
            if (pdc == null)
            {
                if (throwException)
                {
                    throw new Exception("类型" + obj.GetType().Name + "没有可以注册的属性 ");
                }
                else
                {
                    return;
                }
            }
            PropertyDescriptor pd = pdc.Get(propName);
            int index             = pdc.IndexOf(propName);

            if (index < 0)//属性名不存在
            {
                if (throwException)
                {
                    throw new Exception("属性名不存在:" + propName);
                }
                else
                {
                    return;
                }
            }
            if (!objectPropertiesValues.ContainsKey(obj))
            {
                objectPropertiesValues[obj] = new Dictionary <PropertyDescriptor, object>();
            }
            Dictionary <PropertyDescriptor, object> values = objectPropertiesValues[obj];

            values.SetValue(pd, value);
        }
コード例 #2
0
        /// <summary>
        /// 修改缺省值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        public static void SetDefaultValue(this IPropertyObject obj, Object value, bool throwException)
        {
            PropertyDescriptorCollection pdc = obj.GetPropertyDescriptorCollection();

            if (pdc == null)
            {
                RegisteProperties(obj);
            }
            pdc = obj.GetPropertyDescriptorCollection();
            //没有可以注册的属性
            if (pdc == null)
            {
                if (throwException)
                {
                    throw new Exception("类型" + obj.GetType().Name + "没有可以注册的属性 ");
                }
                else
                {
                    return;
                }
            }
            PropertyDescriptor pd = pdc.GetDefault();

            if (pd == null)
            {
                if (throwException)
                {
                    throw new Exception("类型" + obj.GetType().Name + "没有缺省属性");
                }
                else
                {
                    return;
                }
            }
            int index = pdc.IndexOf(pd);

            if (index < 0)
            {
                return;
            }

            if (!objectPropertiesValues.ContainsKey(obj))
            {
                objectPropertiesValues[obj] = new Dictionary <PropertyDescriptor, object>();
            }
            Dictionary <PropertyDescriptor, object> values = objectPropertiesValues[obj];

            values.SetValue(pd, value);
        }
コード例 #3
0
        /// <summary>
        /// 转换字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String Format(this IPropertyObject obj, String sep = ",")
        {
            PropertyDescriptorCollection pdc = obj.GetPropertyDescriptorCollection();

            if (pdc == null)
            {
                return("");
            }
            Comparison <PropertyDescriptor> comparison = (x, y) => x.xh - y.xh;

            pdc.Sort(comparison);

            StringBuilder str = new StringBuilder();

            foreach (PropertyDescriptor pd in pdc)
            {
                if (str.ToString() != "")
                {
                    str.Append(sep);
                }
                Object value = obj.GetValue <Object>(pd.name);
                str.Append(ConvertUtils.ConvertTo <String>(value, pd.format));
            }
            return(str.ToString());
        }
コード例 #4
0
        /// <summary>
        /// 取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="propName"></param>
        /// <returns></returns>
        public static T GetValue <T>(this IPropertyObject obj, String propName = "", bool throwException = false)
        {
            if (propName == "" || propName == null)
            {
                propName = DEFAULT_PROP;
            }
            //如果propName是普通成员,则直接返回成员值
            MemberInfo member = obj.GetType().FindMember(propName);

            if (member != null)
            {
                return(member.FindValue <T>(obj));
            }

            //obj的类型的属性是否已经注册
            PropertyDescriptorCollection pdc = obj.GetPropertyDescriptorCollection();

            if (pdc == null)
            {
                RegisteProperties(obj);
            }
            pdc = obj.GetPropertyDescriptorCollection();
            //没有可以注册的属性
            if (pdc == null)
            {
                if (throwException)
                {
                    throw new Exception("类型" + obj.GetType().Name + "没有可以注册的属性 ");
                }
                else
                {
                    return(default(T));
                }
            }
            PropertyDescriptor pd = pdc.Get(propName);
            int index             = pdc.IndexOf(propName);

            if (index < 0)//属性名不存在
            {
                if (throwException)
                {
                    throw new Exception("属性名不存在:" + propName);
                }
                else
                {
                    return(default(T));
                }
            }
            if (!objectPropertiesValues.ContainsKey(obj))
            {
                //objectPropertiesValues[obj] = new ConcurrentDictionary<PropertyDescriptor, object>();
                objectPropertiesValues[obj] = new Dictionary <PropertyDescriptor, object>();
            }
            Dictionary <PropertyDescriptor, object> values = objectPropertiesValues[obj];

            if (!values.ContainsKey(pd))
            {
                return(default(T));
            }
            return((T)values[pd]);
        }