我需要一种方法来循环遍历每个对象的属性并记录属性名和值。然而,偶尔会遇到一个列表对象的属性,整个过程就会崩溃。我从各种SO帖子中收集了下面的条件,但它每次都返回false。想知道如何测试您的方法属性是否为通用集合类型?
类定义
public class DtccProducer
{
public string ProducerDateOfBirth { get; set; }
public string ProducerGender { get; set; }
public string TransactionType { get; set; }
public List<BigAddress> Addresses { get; set; }
public List<BigCommunicationItem> Communications { get; set; }
}
方法
private static void ParseProducer(object obj)
{
StringBuilder s = new StringBuilder();
Type objType = obj.GetType();
foreach (PropertyInfo info in objType.GetProperties())
{
string key = info.Name;
Type propType = info.PropertyType;
if (propType.IsGenericType && typeof(ICollection<>).IsAssignableFrom(propType.GetGenericTypeDefinition()))
{
// This condition never passes - always evaluates to false
}
else
{
string value = (string) info.GetValue(obj, null);
_sb.AppendLine(key + ": " + value);
}
}
}
&& propType.IsGenericType
- NealR