/// <summary> /// 初始化系统参数配置信息 /// </summary> public void Init() { //所有选项值 List <Options> listOption = ConfigService.GetAllOptions(); ConfigDescription desc = null; //代码现有配置项 foreach (ConfigOption item in AllConfig) { //反射读取配置项ConfigTypeAttribute ConfigAttribute 信息 desc = ConfigDescriptionCache.GetTypeDiscription(item.GetType()); //设置当前配置项的GroupType desc.GroupTypePropertyInfo.SetValue(item, Convert.ChangeType(desc.Group, desc.GroupTypePropertyInfo.PropertyType), null); //每项值信息 List <Options> itemOptions = listOption.Where(e => e.OptionType.Equals(desc.Group, StringComparison.OrdinalIgnoreCase)).ToList(); Options op = null; ConfigAttribute ca = null; 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) { //设置默认值 prop.SetValue(null, Convert.ChangeType(ca.DefaultValue, prop.PropertyType), null); } else { prop.SetValue(null, Convert.ChangeType(op.Value, prop.PropertyType), null); } } } }
/// <summary> /// 获取所有配置信息 /// </summary> /// <returns>所有配置信息</returns> public List <OptionViewModel> GetAllOption(string GroupType = "") { //所有选项值 List <Options> listOption = ConfigService.GetAllOptions(GroupType); //所有分组 List <OptionGroup> listOptionGroup = new List <OptionGroup>(); 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, ImmediateUpdate = desc.ImmediateUpdate, CustomPage = desc.CustomPage }; listOptionGroup.Add(optionGroup); itemOptionViewModel.Group = optionGroup; 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), ValueType = Convert.ToInt32(ca.ValueType).ToString() }; } //必填设置 op.Required = ca.Required; //校验规则 op.ValidateRule = ca.ValidateRule; //悬浮title op.Title = ca.Title; itemOptionViewModel.ListOptions.Add(op); } result.Add(itemOptionViewModel); } return(result.OrderBy(e => e.Group.GroupType).ToList()); }
/// <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); }