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

2 Answers 2

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


            }
Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to do something like this:

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

4 Comments

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 <object> of that class name, but how should i create List<t> in above code is my question.
@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<object> because List<T> has to be the same type of T
yes in single iteration all T of List<T> will be of same type. in each iteration, it will return List<T> where T is of type created from info on row of CloudDBTableList table.
finally found the answer here stackoverflow.com/questions/1151464/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.