在 C# 中使用 JavaScriptSerializer 反序列化通用对象数组
我正在从循环中调用 REST API,并且我的对象名称是在运行时决定的。我可以在这里对一个对象使用反射,但如何获取对象列表?
foreach (CloudDBTableList table in cloudDBTableList)
{
string uri = string.Format(table.URI, "1-Jan-2011");
string result = _dataPullSvcAgent.GetData (baseURI + uri);
string tableClassType = table.TableName + ", " + namespacePrefix;//namespacePrefix is same as assembly name.
Type t = Type.GetType(tableClassType);
JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
//t is only type of object whereas below method returns List<t> how to put it?
var objectList = jsonDeserializer.Deserialize(result, t);
}
return true;
}
I am calling rest API from a loop and my object name is being decided at runtime. I am able to use reflection here for one object but how to get list of object?
foreach (CloudDBTableList table in cloudDBTableList)
{
string uri = string.Format(table.URI, "1-Jan-2011");
string result = _dataPullSvcAgent.GetData (baseURI + uri);
string tableClassType = table.TableName + ", " + namespacePrefix;//namespacePrefix is same as assembly name.
Type t = Type.GetType(tableClassType);
JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
//t is only type of object whereas below method returns List<t> how to put it?
var objectList = jsonDeserializer.Deserialize(result, t);
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Stackoverflow 摇滚。从这个问题找到答案(尽管下面的问题与我的有点不同):-
如何使用反射动态创建通用 C# 对象?
我这样修改我的代码:-
Stackoverflow Rocks. Found answer from this question(though below question was bit dfferent from mine):-
How to dynamically create generic C# object using reflection?
I modified my code like this:-
你应该能够做这样的事情:
You should be able to do something like this: