我需要遍历一个在编译时类型未知的列表。怎么做?以下代码在运行时失败,不允许转换:
Type objType = dataObject.GetType();
List<string> strList = new List<string>();
foreach (PropertyInfo prop in objType.GetProperties())
{
var val = prop.GetValue(dataObject);
if (prop.PropertyType.Name.StartsWith("List")) // Is there a better way?
{
foreach (object lval in (List<object>) val) // Runtime failure (conversion not allowed)
{
strList.Add(lval.ToString());
}
}
...