I need a method to cycle through each object's properties and log the property name and value. On occasion, however, I hit a property that is a list object and the whole thing crashes. I've gather the condition below from various SO posts but it returns false every time. Wondering how to test if your method property is a generic collection type?
Class Definition
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; }
}
Method
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);
}
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire