本文整理汇总了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);
}
}
}
}
示例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 });
}
}
}
示例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
{
}
}
}
示例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 + " ");
}
}
示例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);
}
}
示例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;
}
示例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;
}
}
示例8: PrintConfiguration
private static void PrintConfiguration(IConfiguration configuration)
{
PrintConfigurationSections(configuration.GetChildren());
PrintTypedSettings(configuration);
}
示例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);
}