在 C# 中使用 JavaScriptSerializer 反序列化通用对象数组

发布于 2024-11-28 15:39:05 字数 794 浏览 2 评论 0原文

我正在从循环中调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-12-05 15:39:05

Stackoverflow 摇滚。从这个问题找到答案(尽管下面的问题与我的有点不同):-

如何使用反射动态创建通用 C# 对象?
我这样修改我的代码:-

 foreach (CloudDBTableList table in cloudDBTableList)
            {                
               string uri = string.Format(table.URI, "1-Jan-2011");
               string result  = _dataPullSvcAgent.GetData (baseURI + uri);


               string tableClassType = namespacePrefix + "." + table.SchemaName + "." + table.TableName + ", " + namespacePrefix;//namespacePrefix is same as assembly name.
               Type t = Type.GetType(tableClassType);
               JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
               var L1 = typeof(List<>);
               Type listOfT = L1.MakeGenericType(t);
               var objectList = jsonDeserializer.Deserialize(result, listOfT);


            }

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:-

 foreach (CloudDBTableList table in cloudDBTableList)
            {                
               string uri = string.Format(table.URI, "1-Jan-2011");
               string result  = _dataPullSvcAgent.GetData (baseURI + uri);


               string tableClassType = namespacePrefix + "." + table.SchemaName + "." + table.TableName + ", " + namespacePrefix;//namespacePrefix is same as assembly name.
               Type t = Type.GetType(tableClassType);
               JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
               var L1 = typeof(List<>);
               Type listOfT = L1.MakeGenericType(t);
               var objectList = jsonDeserializer.Deserialize(result, listOfT);


            }
本宫微胖 2024-12-05 15:39:05

你应该能够做这样的事情:

JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
List<CloudDBTableList > list= jsonDeserializer.Deserialize<List<CloudDBTableList>>(cloudDBTableList);

You should be able to do something like this:

JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
List<CloudDBTableList > list= jsonDeserializer.Deserialize<List<CloudDBTableList>>(cloudDBTableList);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文