Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将netcore IConfigurationSection绑定到动态对象_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 将netcore IConfigurationSection绑定到动态对象

C# 将netcore IConfigurationSection绑定到动态对象,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,在我的appSettings.json中,我有一个配置部分,可以包含任何内容,只要它是json有效的。它通常是一组键/值(string/string) 我想在我的代码中得到它,并在控制器调用中返回它 我看了一下源代码(),似乎我注定要使用现成的解决方案 如果我将用例限制为键值对,我可以在IConfigSection中使用AsEnumerable(),这很好。如果我想允许列表,那么我仍然可以解析要查找的键:Number,但是有人有办法轻松地反序列化随机对象吗?或者更好地按原样获取配置部分,而不进行

在我的appSettings.json中,我有一个配置部分,可以包含任何内容,只要它是json有效的。它通常是一组键/值(string/string)

我想在我的代码中得到它,并在控制器调用中返回它

我看了一下源代码(),似乎我注定要使用现成的解决方案

如果我将用例限制为键值对,我可以在IConfigSection中使用AsEnumerable(),这很好。如果我想允许列表,那么我仍然可以解析要查找的键:Number,但是有人有办法轻松地反序列化随机对象吗?或者更好地按原样获取配置部分,而不进行反序列化

比如说

{
 "mySettings": 
 {
   "key1": "value1",
   "key2": "value2",
   "list": [ "item1", "item2", "item3" ],
   "complexObject": {
     "key": "value",
     "anything" :  [{"id": "3", "name": "John"}]
   }
 }
}

如果滥用.NET4动态对象,这是可能的。正如您所说,您可以枚举配置中的所有键,它们都遵循相同的模式。根据您的示例,所有感兴趣的关键点包括:

mySettings null 
mySettings:list null 
mySettings:list:2 item3 
mySettings:list:1 item2 
mySettings:list:0 item1 
mySettings:key3 value3 
mySettings:key2 value2 
mySettings:key1 value1 
mySettings:complexObject null 
mySettings:complexObject:key value 
mySettings:complexObject:anything null 
mySettings:complexObject:anything:0 null 
mySettings:complexObject:anything:0:name John 
mySettings:complexObject:anything:0:id 3 
由此,我们可以构建一个
ExpandoObject
,如下所示:

[HttpGet]
public IActionResult Get([FromServices] IConfiguration config)
{
    var result = new ExpandoObject();

    // retrieve all keys from your settings
    var configs = config.AsEnumerable().Where(_ => _.Key.StartsWith("mySettings"));
    foreach(var kvp in configs) 
    {
        var parent = result as IDictionary<string, object>;
        var path = kvp.Key.Split(':');

        // create or retrieve the hierarchy (keep last path item for later)
        var i = 0;
        for (i = 0; i < path.Length - 1; i++)
        {
            if (!parent.ContainsKey(path[i]))
            {
                parent.Add(path[i], new ExpandoObject());
            }

            parent = parent[path[i]] as IDictionary<string, object>;
        }

        if (kvp.Value == null)
            continue;

        // add the value to the parent
        // note: in case of an array, key will be an integer and will be dealt with later
        var key = path[i];
        parent.Add(key, kvp.Value);
    }

    // at this stage, all arrays are seen as dictionaries with integer keys
    ReplaceWithArray(null, null, result);

    return Ok(result);
}

private void ReplaceWithArray(ExpandoObject parent, string key, ExpandoObject input) 
{
    if (input == null)
        return;

    var dict = input as IDictionary<string, object>;
    var keys = dict.Keys.ToArray();

    // it's an array if all keys are integers
    if (keys.All(k => int.TryParse(k, out var dummy))) {
        var array = new object[keys.Length];
        foreach(var kvp in dict) {
            array[int.Parse(kvp.Key)] = kvp.Value;
            // Edit: If structure is nested deeper we need this next line 
            ReplaceWithArray(input, kvp.Key, kvp.Value as ExpandoObject);
        }

        var parentDict = parent as IDictionary<string, object>;
        parentDict.Remove(key);
        parentDict.Add(key, array);
    }
    else
    {
        foreach (var childKey in dict.Keys.ToList())
        {
            ReplaceWithArray(input, childKey, dict[childKey] as ExpandoObject);
        }
    }
}

你不能。它被称为“强类型配置”是有原因的。你可以,使用动态对象。工作起来很有魅力!但是有一个问题,在这里使用ExpandooObject而不是Dictionary有什么好处?我运行了测试,这两种方法似乎都有效。使用
ExpandoObject
可以将
result
用作真正的动态对象,这样您就可以使用点语法访问任何成员(请参见我答案末尾的示例)。如果不需要访问控制器中的单个属性,并且只需要整个对象,则可以使用字典。
dynamic asDym = result;
string name = asDym.mySettings.complexObject.anything[0].name;