Given this:
class InvoiceHeader {
public int InvoiceHeaderId { get; set; }
IList<InvoiceDetail> LineItems { get; set; }
}
I’m currently using this code to detect if a class has a collection property:
void DetectCollection(object modelSource)
{
Type modelSourceType = modelSource.GetType();
foreach (PropertyInfo p in modelSourceType.GetProperties())
{
if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(IList<>))
{
System.Windows.Forms.MessageBox.Show(p.Name);
}
}
}
Is there a general way to detect if the LineItems is an enumerable type? Some will use other enumerable type (e.g. ICollection), not an IList.
Your code doesn’t actually check if the properties are
Enumerabletypes but if they are generic IList’s. Try this:Or this