Ywdsoft.Utility.ConfigHandler.ConfigDescriptionCache.GetTypeDiscription C# (CSharp) Method

GetTypeDiscription() public static method

类型获取Attribute信息
public static GetTypeDiscription ( Type type ) : ConfigDescription
type System.Type Type
return ConfigDescription
        public static ConfigDescription GetTypeDiscription(Type type)
        {
            ConfigDescription description = s_typeInfoDict[type.FullName] as ConfigDescription;
            if (description == null)
            {
                ConfigTypeAttribute typeAttr = type.GetMyAttribute<ConfigTypeAttribute>();
                description = new ConfigDescription { Group = typeAttr.Group, GroupCn = typeAttr.GroupCn, ImmediateUpdate = typeAttr.ImmediateUpdate };
                if (!string.IsNullOrEmpty(description.Group))
                {
                    //获取分组类型属
                    description.GroupTypePropertyInfo = type.GetProperty("GroupType");

                    //获取静态配置项属性集合
                    PropertyInfo[] propertyInfos = type.GetProperties(s_flag);
                    int length = propertyInfos.Length;
                    description.StaticPropertyInfo = new List<PropertyInfo>(length);
                    Dictionary<string, ConfigAttribute> dict = new Dictionary<string, ConfigAttribute>(length, StringComparer.OrdinalIgnoreCase);

                    ConfigAttribute configAttr = null;
                    Type ValueType = typeof(ConfigValueType);
                    foreach (PropertyInfo prop in propertyInfos)
                    {
                        //将配置了ConfigAttribute的静态属性添加到缓存,其它排除
                        configAttr = prop.GetMyAttribute<ConfigAttribute>();
                        if (configAttr != null)
                        {
                            if (string.IsNullOrEmpty(configAttr.Name))
                            {
                                //显示中文必须填写
                                continue;
                            }
                            if (string.IsNullOrEmpty(configAttr.Key))
                            {
                                //默认为属性值
                                configAttr.Key = prop.Name;
                            }
                            //设置值类型
                            if (!Enum.IsDefined(ValueType, configAttr.ValueType))
                            {
                                SetConfigValueType(configAttr, prop.PropertyType);
                            }
                            dict[prop.Name] = configAttr;
                            description.StaticPropertyInfo.Add(prop);
                        }
                    }
                    description.MemberDict = dict;
                }

                // 添加到缓存字典
                s_typeInfoDict[type.FullName] = description;
            }
            return description;
        }

Usage Example

Example #1
0
        /// <summary>
        /// 保存时 对当前配置项进行赋值
        /// </summary>
        /// <param name="item">当前配置项</param>
        /// <param name="ListOptions">配置项值</param>
        public void SetValue(ConfigOption item, List <Options> ListOptions)
        {
            var     desc   = ConfigDescriptionCache.GetTypeDiscription(item.GetType());
            Options option = null;

            foreach (PropertyInfo prop in desc.StaticPropertyInfo)
            {
                option = ListOptions.First(e => e.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
                if (option == null)
                {
                    //不存在该配置项,则清空当前值
                    prop.SetValue(null, Convert.ChangeType(null, prop.PropertyType), null);
                }
                else
                {
                    prop.SetValue(null, Convert.ChangeType(option.Value, prop.PropertyType), null);
                }
            }
        }
All Usage Examples Of Ywdsoft.Utility.ConfigHandler.ConfigDescriptionCache::GetTypeDiscription