Reputation: 4551
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;
}
Upvotes: 1
Views: 1514
Reputation: 4551
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);
}
Upvotes: 3
Reputation: 63972
You should be able to do something like this:
JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
List<CloudDBTableList > list= jsonDeserializer.Deserialize<List<CloudDBTableList>>(cloudDBTableList);
Upvotes: 0