Is there a general way to detect if a property's type is an enumerable type?

Your code doesn't actually check if the properties are Enumerable types but if they are generic IList's. Try this:

if(typeof(IEnumerable).IsAssignableFrom(p.PropertyType))
{
   System.Windows.Forms.MessageBox.Show(p.Name);
}

Or this

if (p.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)))
{
    System.Windows.Forms.MessageBox.Show(p.Name);
}

if (invoiceHeader.LineItems is IEnumerable) {
    // LineItems implements IEnumerable
}

This does not work if the type of the invoiceHeader is unknown at compile time. In that case I would like to know why there isn't a common interface, because the use of reflection to find a collection property is quite dubious.

Tags:

C#