Ejemplo n.º 1
0
        /// <summary>
        /// 获取所有配置信息
        /// </summary>
        /// <returns>所有配置信息</returns>
        public List <OptionViewModel> GetAllOption(string GroupType = "")
        {
            //所有选项值
            List <Options> listOption = ConfigService.GetAllOptions(GroupType);

            IEnumerable <Tags> listTags = TagService.GetTags(ConfigHandler);

            IEnumerable <ConfigOption> listConfigs = AllConfig;

            if (!string.IsNullOrEmpty(GroupType))
            {
                listConfigs = AllConfig.Where(e => e.GroupType.Equals(GroupType, StringComparison.OrdinalIgnoreCase));
            }

            ConfigDescription desc = null;

            //分组信息
            OptionGroup     optionGroup = null;
            Options         op          = null;
            ConfigAttribute ca          = null;

            List <OptionViewModel> result = new List <OptionViewModel>();
            OptionViewModel        itemOptionViewModel = null;

            //代码现有配置项
            foreach (ConfigOption item in listConfigs)
            {
                //反射读取配置项ConfigTypeAttribute  ConfigAttribute 信息
                desc = ConfigDescriptionCache.GetTypeDiscription(item.GetType());

                itemOptionViewModel = new OptionViewModel();
                optionGroup         = new OptionGroup {
                    GroupType = desc.Group, GroupName = desc.GroupCn, CustomPage = desc.CustomPage
                };
                optionGroup.ImmediateUpdate      = desc.ImmediateUpdate;
                itemOptionViewModel.Group        = optionGroup;
                itemOptionViewModel.FunctionType = desc.FunctionType;
                itemOptionViewModel.ListOptions  = new List <Options>();

                //每项值信息
                List <Options> itemOptions = listOption.Where(e => e.OptionType.Equals(desc.Group, StringComparison.OrdinalIgnoreCase)).ToList();

                foreach (PropertyInfo prop in desc.StaticPropertyInfo)
                {
                    op = itemOptions.FirstOrDefault(e1 => e1.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
                    ca = desc.MemberDict[prop.Name];

                    if (op == null)
                    {
                        op = new Options
                        {
                            OptionType = desc.Group,
                            OptionName = ca.Name,
                            Key        = prop.Name,
                            Value      = Convert.ToString(ca.DefaultValue)
                        };
                    }
                    //必填设置
                    op.Required = ca.Required;
                    //校验规则
                    op.ValidateRule = ca.ValidateRule;
                    //悬浮title
                    op.Title      = ca.Title;
                    op.Valuetype  = Convert.ToInt32(ca.ValueType).ToString();
                    op.OptionName = ca.Name;
                    op.DataSource = ca.DataSource == null ? null : JsonConvert.DeserializeObject(ca.DataSource);
                    op.FormatDate = ca.FormatDate;
                    itemOptionViewModel.ListOptions.Add(op);
                    itemOptionViewModel.TagList = listTags.Where(e => e.SourceId == itemOptionViewModel.Group.GroupType).ToList();
                }
                result.Add(itemOptionViewModel);
            }
            return(result.OrderBy(e => e.Group.GroupType).ToList());
        }
Ejemplo n.º 2
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;
            Dictionary <ConfigAttribute, MethodInfo> dictAttr = null;

            if (description == null)
            {
                dict_AttrMethodInfo[type] = null;
                ConfigTypeAttribute typeAttr = type.GetMyAttribute <ConfigTypeAttribute>();
                description = new ConfigDescription {
                    Group = typeAttr.Group, GroupCn = typeAttr.GroupCn, ImmediateUpdate = typeAttr.ImmediateUpdate, CustomPage = typeAttr.CustomPage, FunctionType = typeAttr.FunctionType
                };
                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);
                            }
                            //下拉选项数据源
                            if ((configAttr.ValueType == ConfigValueType.Select || configAttr.ValueType == ConfigValueType.MultiSelect) && !string.IsNullOrEmpty(configAttr.DataSource))
                            {
                                var arrAssemble = configAttr.DataSource.Split(',');
                                if (arrAssemble.Length == 2)
                                {
                                    var index      = arrAssemble[1].LastIndexOf(".");
                                    var className  = arrAssemble[1].Substring(0, index);
                                    var methodName = arrAssemble[1].Substring(index + 1);
                                    //反射调用方法
                                    var MethodInfo = Assembly.Load(arrAssemble[0]).GetType(className).GetMethod(methodName);

                                    dictAttr = dict_AttrMethodInfo[type];
                                    if (dictAttr == null)
                                    {
                                        dictAttr = new Dictionary <ConfigAttribute, MethodInfo>();
                                        dict_AttrMethodInfo[type] = dictAttr;
                                    }
                                    dictAttr.Add(configAttr, MethodInfo);
                                }
                                configAttr.DataSource = null;
                            }

                            dict[prop.Name] = configAttr;
                            description.StaticPropertyInfo.Add(prop);
                        }
                    }
                    description.MemberDict = dict;
                }

                // 添加到缓存字典
                s_typeInfoDict[type.FullName] = description;
            }
            dictAttr = dict_AttrMethodInfo[type];
            if (dictAttr != null)
            {
                foreach (var attr in dictAttr.Keys)
                {
                    var result = dictAttr[attr].FastInvoke(null);
                    if (result != null)
                    {
                        attr.DataSource = Newtonsoft.Json.JsonConvert.SerializeObject(result);
                    }
                }
            }
            return(description);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 动态设置属性的默认值,初始化时调用
 /// </summary>
 /// <param name="ca">属性</param>
 /// <remarks>用于解决默认值是经过其它条件计算出来,不能初始化设置的问题</remarks>
 public virtual void SetDefaultValue(ConfigAttribute ca)
 {
 }