【发布时间】:2016-04-26 15:53:35
【问题描述】:
我正在尝试检查JArray 中包含的dynamic 对象上的每个属性:
Newtonsoft.Json.Linq.JArray feeds = Newtonsoft.Json.Linq.JArray.Parse(response.Content);
if (feeds.Any())
{
PropertyDescriptorCollection dynamicProperties = TypeDescriptor.GetProperties(feeds.First());
foreach (dynamic feed in feeds)
{
object[] args = new object[dynamicProperties.Count];
int i = 0;
foreach (PropertyDescriptor prop in dynamicProperties)
{
args[i++] = feed.GetType().GetProperty(prop.Name).GetValue(feed, null);
}
yield return (T)Activator.CreateInstance(typeof(T), args);
}
}
当我尝试访问feed.GetType().GetProperty(prop.Name).GetValue(feed, null); 时,它告诉我feed.GetType().GetProperty(prop.Name); 为空。
JSON 结构如下:
[
{
"digitalInput.field.channel":"tv",
"digitalInput.field.comment":"archive",
"count(digitalInput.field.comment)":130
}
]
有人可以帮我吗?
【问题讨论】:
-
你也可以添加你的 JSON 数据 - 否则每个人都在黑暗中射击。
-
我不知道你为什么要在你的循环中再次向上移动树。你到底想实现什么并且在 prop.GetValue() 和 prop.GetType() 之外?