Exemplo n.º 1
0
        /// <summary>
        /// 注册类属性
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="pds"></param>
        public static void RegisteProperties(this IPropertyObject obj, PropertyDescriptorCollection pds = null)
        {
            if (typeProperties.ContainsKey(obj.GetType()))
            {
                return;
            }
            if (pds == null || pds.Count <= 0)
            {
                PropertiesAttribute attr = obj.GetType().FindAttribute <PropertiesAttribute>(false);
                if (attr == null || attr.items == null || attr.items.Length <= 0)
                {
                    return;
                }
                pds = new PropertyDescriptorCollection();
                pds.AddRange(attr.items);
            }

            PropertyDescriptorCollection pdc = new PropertyDescriptorCollection();

            pdc.AddRange(pds);
            typeProperties[obj.GetType()] = pdc;
            if (!objectPropertiesValues.ContainsKey(obj))
            {
                objectPropertiesValues[obj] = new Dictionary <PropertyDescriptor, object>();
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 获取属性集
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static PropertyDescriptorCollection GetPropertyDescriptorCollection(this IPropertyObject obj)
 {
     if (typeProperties.ContainsKey(obj.GetType()))
     {
         return(typeProperties[obj.GetType()]);
     }
     return(null);
 }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public static Dictionary <string, ObjectProperty> GetProperties(IPropertyObject obj)
        {
            var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast <PropertyDescriptor>();

            Dictionary <string, ObjectProperty> dictionary = new Dictionary <string, ObjectProperty>();

            foreach (var property in properties)
            {
                dictionary.Add(property.Name.ToLowerInvariant(), new ObjectProperty(property, obj));
            }

            return(dictionary);
        }
Exemplo n.º 6
0
        public static Dictionary <string, ObjectProperty> InitProperties(IPropertyObject obj, XElement element)
        {
            var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast <PropertyDescriptor>();

            Dictionary <string, ObjectProperty> dictionary = new Dictionary <string, ObjectProperty>();

            foreach (var property in properties)
            {
                ObjectProperty objProperty = new ObjectProperty(property, obj);
                dictionary.Add(property.Name.ToLowerInvariant(), objProperty);

                //set the value of the property to the default value if there is one
                foreach (var ini in property.Attributes.OfType <HasDefaultValue>())
                {
                    objProperty.TrySetValue(ini.defaultValue);
                    break;
                }
            }

            if (element != null)
            {
                //go through all the attributes in the xml element
                //and set the value of the matching property if it is initializable
                foreach (XAttribute attribute in element.Attributes())
                {
                    ObjectProperty property = null;
                    if (!dictionary.TryGetValue(attribute.Name.ToString().ToLowerInvariant(), out property))
                    {
                        continue;
                    }
                    if (!property.Attributes.OfType <HasDefaultValue>().Any())
                    {
                        continue;
                    }
                    property.TrySetValue(attribute.Value);
                }
            }

            return(dictionary);
        }
Exemplo n.º 7
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]);
        }