简体   繁体   中英

How to access value from a list from an object type using reflection

I have a service method which will return me an object type.

var serviceResponse = InvokeServiceMethod(parameters);

I am able to access data from it using reflection for ex:

serviceResponse.GetType().GetProperty("NumberOfRecords").GetValue(serviceResponse, null)

In the response I have a list but I am unable to access the values. Its throwing error 'Index was outside the bounds of the array' . I am able to see the data inside. Following is what I am trying to do.

PropertyInfo summary= serviceResponse.GetType().GetProperty("SummaryList");
PropertyInfo test = summary.PropertyType.GetGenericArguments()[0].GetProperty("Name");

Thanks Dima for your suggestion. It worked.. Following is what I did to solve this issue..

dynamic collection = serviceResponse.GetType().GetProperty("SummaryList").GetValue(serviceResponse, null);
foreach (var item in collection)
{
    var value = item.Name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM