Exemplo n.º 1
0
        /// <summary>
        /// 类型获取Attribute信息
        /// </summary>
        /// <param name="type">Type</param>
        /// <returns></returns>
        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, CustomPage = typeAttr.CustomPage
                };
                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);

                    //调用动态设置默认值方法
                    MethodInfo mi           = type.GetMethod("SetDefaultValue");
                    object     typeInstance = Activator.CreateInstance(type);
                    object[]   objParams    = new object[1];

                    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);
                            }
                            //设置默认值
                            if (mi != null)
                            {
                                objParams[0] = configAttr;
                                mi.Invoke(typeInstance, objParams);
                            }
                            dict[prop.Name] = configAttr;
                            description.StaticPropertyInfo.Add(prop);
                        }
                    }
                    description.MemberDict = dict;
                }

                // 添加到缓存字典
                s_typeInfoDict[type.FullName] = description;
            }
            return(description);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 动态设置属性的默认值,初始化时调用
 /// </summary>
 /// <param name="ca">属性</param>
 /// <remarks>用于解决默认值是经过其它条件计算出来,不能初始化设置的问题</remarks>
 public virtual void SetDefaultValue(ConfigAttribute ca)
 {
 }