小编use*_*539的帖子

从未知类型字典中获取键和值列表,而不使用动态

我正在尝试将字典转换为键值对,因此我可以对其进行一些特殊的解析并将其存储为字符串格式.我正在使用Unity,因此我无法使用dynamic关键字.这是我的设置

我有一些类,我正在迭代它的属性并操纵它们的值并将它们放在一个新的字典中.问题是我不知道如何从字典中获取密钥和值,而不使用动态技巧我不知道类型.有什么想法吗?我需要对列表做同样的事情.

    Type t = GetType();
    Dictionary<string, object> output = new Dictionary<string, object>();
    foreach(PropertyInfo info in t.GetProperties())
    {
        object o = info.GetValue(this, null);
        if(info.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            foreach(object key in o) //not valid
            {
                object val = DoSomething(o[key]);//not valid
                output[key] = val;
            }
        }
        else if(info.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
        {

        }
    }
    return output;
Run Code Online (Sandbox Code Playgroud)

c# dictionary

2
推荐指数
1
解决办法
1241
查看次数

标签 统计

c# ×1

dictionary ×1