[英]How to access value from a list from an object type using reflection
我有一個服務方法,它將返回一個對象類型。
var serviceResponse = InvokeServiceMethod(parameters);
我可以使用反射來訪問數據,例如:
serviceResponse.GetType().GetProperty("NumberOfRecords").GetValue(serviceResponse, null)
在響應中,我有一個列表,但是我無法訪問這些值。 它的拋出錯誤“索引超出了數組的范圍” 。 我能夠看到里面的數據。 以下是我正在嘗試做的事情。
PropertyInfo summary= serviceResponse.GetType().GetProperty("SummaryList");
PropertyInfo test = summary.PropertyType.GetGenericArguments()[0].GetProperty("Name");
感謝Dima的建議。 它起作用了。以下是我為解決此問題所做的工作。
dynamic collection = serviceResponse.GetType().GetProperty("SummaryList").GetValue(serviceResponse, null);
foreach (var item in collection)
{
var value = item.Name;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.