c# - 将嵌套类转换为字典

标签 c# dictionary recursion traversal

我想将嵌套类转换为漂亮的键/值字典,并保留每个属性的类名和嵌套路径,以便以后轻松搜索。

这是我的示例类:

var agreement = new Agreement
            {
                ID = 101,
                Description = "Convert to dictionary",
                Customer = new Customer
                {
                    FirstName = "John",
                    LastName = "Smith",
                    Age = 30,
                    Address = new List<Address>() 
                    { 
                        new Address 
                        {
                            Name = "Los Angeles 1",
                            ZipCode = 25437
                        }, 
                        new Address
                        {
                            Name = "New York 25",
                            ZipCode = 25441
                        }
                    }
                }
            };

这是我在字典中预期的键/值输出:

KEY                                    VALUE
Agreement.ID:                          101
Agreement.Description:                 Convert to dictionary
Agreement.Customer.FirstName:          John
Agreement.Customer.LastName:           Smith
Agreement.Customer.Age:                30
Agreement.Customer.Address[0].Name:    Los Angeles 1
Agreement.Customer.Address[0].ZipCode: 25437
Agreement.Customer.Address[1].Name:    New York 25
Agreement.Customer.Address[1].ZipCode: 25441

有人知道我如何实现这一点吗?

最佳答案

(抱歉,我没有时间测试这个。)

您可以使用反射和递归编写解决方案。像这样的东西,在下面。

您需要添加空检查和其他退出情况,否则您将很快陷入无限循环。 这只是为了帮助您入门。

public Dictionary<string, string> MapToDictionary(object source, string name)
{
    var dictionary = new Dictionary<string, string>();
    MapToDictionaryInternal(dictionary, source, name);
    return dictionary;
}

private void MapToDictionaryInternal(
    Dictionary<string, string> dictionary, object source, string name)
{
    var properties = source.GetType().GetProperties();
    foreach(var p in properties) 
    {
        var key = name + "." + p.Name;
        object value = p.GetValue(source, null);
        Type valueType = value.GetType();

        if (valueType.IsPrimitive || valueType == typeof (String))
        {
            dictionary[key] = value.ToString();
        }
        else if (value is IEnumerable)
        {
            var i = 0;
            foreach (object o in (IEnumerable) value)
            {
                MapToDictionaryInternal(dictionary, o, key + "[" + i + "]");
                i++;
            }
        }
        else
        {
            MapToDictionaryInternal(dictionary, value, key);
        }
    }
}

这样调用它:

Dictionary<string, string> dictionary2 = MapToDictionary(agreement, "Agreement");

关于c# - 将嵌套类转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314649/

相关文章:

javascript - 没有得到整个扁平化的 javascript 对象结构

java - 查找数组中有多少个不同的值

python - 如果没有命令行参数,则读取 stdin Python

c# - 如何检查字典中是否存在键值对

algorithm - 有什么只能通过递归来实现的吗?

javascript - Unity3D - 错误提示意外符号 `extends'

python - 如何处理包含 2^50 个元素的字典变量?

c# - 我可以压缩 Response.write 吗?

c# - 是否有与 C# 中的 F# Seq.windowed 等效的功能?

c# - 选择多个日期的计数