【发布时间】:2014-08-17 21:46:28
【问题描述】:
我正在尝试将字典转换为键值对,以便对其进行特殊解析并将其存储为字符串格式。我正在使用 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;
【问题讨论】:
标签: c# dictionary