当前位置: 首页>>代码示例>>C#>>正文


C# IConfiguration.GetChildren方法代码示例

本文整理汇总了C#中IConfiguration.GetChildren方法的典型用法代码示例。如果您正苦于以下问题:C# IConfiguration.GetChildren方法的具体用法?C# IConfiguration.GetChildren怎么用?C# IConfiguration.GetChildren使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IConfiguration的用法示例。


在下文中一共展示了IConfiguration.GetChildren方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConfigurationConfigurationProvider

        public ConfigurationConfigurationProvider(string prefix, IConfiguration source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (prefix == null)
            {
                foreach (var pair in source.GetChildren())
                {
                    Data.Add(pair.Key, pair.Value);
                }
            }
            else
            {
                var pairs = source.GetChildren();

                foreach (var pair in pairs)
                {
                    if (pair.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                    {
                        Data.Add(pair.Key.Substring(prefix.Length), pair.Value);
                    }
                }
            }
        }
开发者ID:nietras,项目名称:benchmarks,代码行数:27,代码来源:ConfigurationConfigurationProvider.cs

示例2: BindDictionary

        private static void BindDictionary(object dictionary, Type dictionaryType, IConfiguration config)
        {
            var typeInfo = dictionaryType.GetTypeInfo();

            // IDictionary<K,V> is guaranteed to have exactly two parameters
            var keyType = typeInfo.GenericTypeArguments[0];
            var valueType = typeInfo.GenericTypeArguments[1];

            if (keyType != typeof(string))
            {
                // We only support string keys
                return;
            }

            var addMethod = typeInfo.GetDeclaredMethod("Add");
            foreach (var child in config.GetChildren())
            {
                var item = BindInstance(
                    type: valueType,
                    instance: null,
                    config: child);
                if (item != null)
                {
                    var key = child.Key;
                    var section = config as IConfigurationSection;
                    if (section != null)
                    {
                        // Remove the parent key and : delimiter to get the configurationSection's key
                        key = key.Substring(section.Key.Length + 1);
                    }

                    addMethod.Invoke(dictionary, new[] { key, item });
                }
            }
        }
开发者ID:pgrudzien12,项目名称:Configuration,代码行数:35,代码来源:ConfigurationBinder.cs

示例3: BindCollection

        private static void BindCollection(object collection, Type collectionType, IConfiguration config)
        {
            var typeInfo = collectionType.GetTypeInfo();

            // ICollection<T> is guaranteed to have exacly one parameter
            var itemType = typeInfo.GenericTypeArguments[0];
            var addMethod = typeInfo.GetDeclaredMethod("Add");

            foreach (var section in config.GetChildren())
            {
                try
                {
                    var item = BindInstance(
                        type: itemType,
                        instance: null,
                        config: section);
                    if (item != null)
                    {
                        addMethod.Invoke(collection, new[] { item });
                    }
                }
                catch
                {
                }
            }
        }
开发者ID:pgrudzien12,项目名称:Configuration,代码行数:26,代码来源:ConfigurationBinder.cs

示例4: DumpConfig

 private static async Task DumpConfig(HttpResponse response, IConfiguration config, string indentation = "")
 {
     foreach (var child in config.GetChildren())
     {
         await response.WriteAsync(indentation + "[" + child.Key + "] " + config[child.Key] + "\r\n");
         await DumpConfig(response, child, indentation + "  ");
     }
 }
开发者ID:leloulight,项目名称:Entropy,代码行数:8,代码来源:Startup.cs

示例5: IncludedConfigurationProvider

 public IncludedConfigurationProvider(IConfiguration source)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     int pathStart = 0;
     var section = source as IConfigurationSection;
     if (section != null)
     {
         pathStart = section.Path.Length + 1;
     }
     foreach (var child in source.GetChildren())
     {
         AddSection(child, pathStart);
     }
 }
开发者ID:leloulight,项目名称:Hosting,代码行数:17,代码来源:IncludedConfigurationProvider.cs

示例6: RecurseConfig

        private static Dictionary<string, string> RecurseConfig(IConfiguration source)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            foreach (var child in source.GetChildren())
            {
                if (child.GetChildren().Count() != 0)
                {
                    result = result.Concat(RecurseConfig(child)).GroupBy(d => d.Key).ToDictionary(d => d.Key, d => d.First().Value);
                }

                if (child.GetChildren().Count() != 0 && string.IsNullOrEmpty(child.Value))
                {
                    continue;
                }

                result.Add(child.Path, child.Value);

            }

            return result;
        }
开发者ID:Sphiecoh,项目名称:dotnet.microservice,代码行数:22,代码来源:AppConfig.cs

示例7: BindInstance

        private static object BindInstance(Type type, object instance, IConfiguration config)
        {
            var section = config as IConfigurationSection;
            var configValue = section?.Value;
            if (configValue != null)
            {
                // Leaf nodes are always reinitialized
                return ReadValue(type, configValue, section);
            }
            else
            {
                if (config.GetChildren().Count() != 0)
                {
                    if (instance == null)
                    {
                        var typeInfo = type.GetTypeInfo();
                        if (typeInfo.IsInterface || typeInfo.IsAbstract)
                        {
                            throw new InvalidOperationException(Resources.FormatError_CannotActivateAbstractOrInterface(type));
                        }

                        var hasDefaultConstructor = typeInfo.DeclaredConstructors.Any(ctor => ctor.IsPublic && ctor.GetParameters().Length == 0);
                        if (!hasDefaultConstructor)
                        {
                            throw new InvalidOperationException(Resources.FormatError_MissingParameterlessConstructor(type));
                        }

                        try
                        {
                            instance = Activator.CreateInstance(type);
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidOperationException(Resources.FormatError_FailedToActivate(type), ex);
                        }
                    }

                    // See if its a Dictionary
                    var collectionInterface = FindOpenGenericInterface(typeof(IDictionary<,>), type);
                    if (collectionInterface != null)
                    {
                        BindDictionary(instance, collectionInterface, config);
                    }
                    else
                    {
                        // See if its an ICollection
                        collectionInterface = FindOpenGenericInterface(typeof(ICollection<>), type);
                        if (collectionInterface != null)
                        {
                            BindCollection(instance, collectionInterface, config);
                        }
                        // Something else
                        else
                        {
                            Bind(config, instance);
                        }
                    }
                }
                return instance;
            }
        }
开发者ID:pgrudzien12,项目名称:Configuration,代码行数:61,代码来源:ConfigurationBinder.cs

示例8: PrintConfiguration

		private static void PrintConfiguration(IConfiguration configuration)
		{
			PrintConfigurationSections(configuration.GetChildren());
			PrintTypedSettings(configuration);
		}
开发者ID:gusztavvargadr,项目名称:aspnet-Configuration.Contrib,代码行数:5,代码来源:Program.cs

示例9: GetByName

        private static CacheManagerConfiguration GetByName(IConfiguration configuration, string name)
        {
            var section = configuration.GetChildren().FirstOrDefault(p => p[ConfigurationName] == name);
            if (section == null)
            {
                throw new InvalidOperationException(
                    $"CacheManager configuration for name '{name}' not found.");
            }

            return GetFromConfiguration(section);
        }
开发者ID:yuzukwok,项目名称:CacheManager,代码行数:11,代码来源:MicrosoftConfigurationExtensions.cs


注:本文中的IConfiguration.GetChildren方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。