Esempio n. 1
0
        /// <summary>
        /// 获取配置值集合
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="configSetting">配置节</param>
        /// <param name="name">值名</param>
        /// <param name="initializer">对元素进行初始化</param>
        /// <returns>配置值集合</returns>
        public static ICollection <T> GetRequiredSettingsWithInitializer <T>(this IConfigSetting configSetting, string name, Action <T> initializer) where T : IConfigSettingElement, new()
        {
            var list = new List <T>();

            if (configSetting != null)
            {
                var settings = configSetting.GetChildSettings(name);
                if (settings != null && settings.Length > 0)
                {
                    foreach (var item in settings)
                    {
                        var t = item.ToSetting <T>();
                        if (initializer != null)
                        {
                            initializer(t);
                        }
                        list.Add(t);
                    }
                }
                else
                {
                    throw new ConfigException(string.Format("必须配置节点 {0}", name));
                }
            }
            return(list);
        }
Esempio n. 2
0
        private static void ConfigSettingInherit(IConfigSetting configSetting, List <IConfigSetting> loopList, List <IConfigSetting> doneList)
        {
            if (configSetting == null)
            {
                return;
            }
            var inheritPath = configSetting.Property.TryGetPropertyValue(ConfigInheritPropertyName);

            if (string.IsNullOrEmpty(inheritPath))
            {
                return;
            }
            var recursive = false;
            var inherit   = false;

            if (!inheritPath.StartsWith(".."))
            {
                if (!bool.TryParse(inheritPath, out recursive))
                {
                    if (inheritPath.StartsWith("{") && inheritPath.EndsWith("}"))
                    {
                        var configInherit = JsonHelper.FromJson <ConfigInheritInternal>(inheritPath, false);
                        if (configInherit != null)
                        {
                            inherit     = true;
                            inheritPath = configInherit.Base;
                            recursive   = configInherit.Recursive;
                        }
                    }
                }
            }
            else
            {
                inherit = true;
            }
            if (!inherit && !recursive)
            {
                return;
            }
            if (loopList.Contains(configSetting))
            {
                throw new ConfigException("配置节循环继承 " + configSetting.Name);
            }
            if (doneList.Contains(configSetting))
            {
                return;
            }
            loopList.Add(configSetting);
            if (recursive)
            {
                if (configSetting.Children > 0)
                {
                    foreach (var setting in configSetting.GetChildSettings())
                    {
                        ConfigSettingInherit(setting, loopList, doneList);
                    }
                }
            }
            if (inherit)
            {
                if (!string.IsNullOrEmpty(inheritPath))
                {
                    var inheritSetting = configSetting.GetChildSetting(inheritPath);
                    if (inheritSetting != null)
                    {
                        ConfigSettingInherit(inheritSetting, loopList, doneList);
                        configSetting.CopyFrom(inheritSetting);
                    }
                }
            }
            loopList.Remove(configSetting);
            doneList.Add(configSetting);
        }