1

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;
        }
Anand
  • 4,523
  • 10
  • 47
  • 72

2 Answers2

3

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);


            }
Community
  • 1
  • 1
Anand
  • 4,523
  • 10
  • 47
  • 72
0

You should be able to do something like this:

JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer();
List<CloudDBTableList > list= jsonDeserializer.Deserialize<List<CloudDBTableList>>(cloudDBTableList);
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Actually, from CloudDBTableList, each row is giving me different C# Class names, and URI, I am calling those URI, and URI is returning me List of of that class name, but how should i create List in above code is my question. – Anand Aug 09 '11 at 11:53
  • @Sutikshan Dubey I see, in that case I'll let someone else answer your question but my impression is that you can't to that. The only way -that I know of- to do something like that is just have a List because List has to be the same type of T – Icarus Aug 09 '11 at 12:05
  • yes in single iteration all T of List will be of same type. in each iteration, it will return List where T is of type created from info on row of CloudDBTableList table. – Anand Aug 09 '11 at 12:16
  • finally found the answer here http://stackoverflow.com/questions/1151464/how-to-dynamically-create-generic-c-object-using-reflection – Anand Aug 09 '11 at 12:39