75

Below is some code I use to get the initial state of all public properties in a class for IsDirty checking.

What's the easiest way to see if a property is IEnumerable?

Cheers,
Berryl

  protected virtual Dictionary<string, object> _GetPropertyValues()
    {
        return _getPublicPropertiesWithSetters()
            .ToDictionary(pi => pi.Name, pi => pi.GetValue(this, null));
    }

    private IEnumerable<PropertyInfo> _getPublicPropertiesWithSetters()
    {
        return GetType().GetProperties().Where(pi => pi.CanWrite);
    }

UPDATE

What I wound up doing was adding a few library extensions as follows

    public static bool IsNonStringEnumerable(this PropertyInfo pi) {
        return pi != null && pi.PropertyType.IsNonStringEnumerable();
    }

    public static bool IsNonStringEnumerable(this object instance) {
        return instance != null && instance.GetType().IsNonStringEnumerable();
    }

    public static bool IsNonStringEnumerable(this Type type) {
        if (type == null || type == typeof(string))
            return false;
        return typeof(IEnumerable).IsAssignableFrom(type);
    }
Berryl
  • 12,044
  • 22
  • 92
  • 175

3 Answers3

94
if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string))
Hans Kesting
  • 36,179
  • 8
  • 79
  • 104
Fyodor Soikin
  • 74,930
  • 8
  • 118
  • 160
21

I agree with Fyodor Soikin but the fact that is Enumerable does not mean that is only a Collection since string is also Enumerable and returns the characters one by one...

So i suggest using

if (typeof(ICollection<>).IsAssignableFrom(pi.PropertyType))
sstauross
  • 2,342
  • 2
  • 27
  • 49
  • 1
    You are right about the string of course, but your solution fails (try it with List()). See my update for the code I wound up using. Cheers! – Berryl Mar 16 '13 at 12:18
  • 1
    This fails because no constructed type (like `List`) can be assigned to a generic type ( `ICollection<>`) (in fact, you can't declare a variable of type `ICollection<>`). So better use `typeof(ICollection)` (as suggested by the editor), which will also make it work for non-generic collections. – René Vogt Sep 19 '17 at 12:45
  • Very much indeed – Joanvo Jan 15 '18 at 11:18
8

Try

private bool IsEnumerable(PropertyInfo pi)
{
   return pi.PropertyType.IsSubclassOf(typeof(IEnumerable));
}
Jonas Stensved
  • 12,991
  • 4
  • 47
  • 71
Steve Danner
  • 21,452
  • 7
  • 39
  • 51
  • 3
    I recently noticed that `x.IsSubClassOf(y)` will return false if x == y. In this situation, if the property happened to actually be of type `IEnumerable`, then the function would return false. – Dr. Wily's Apprentice Aug 25 '10 at 21:02
  • 1
    That is interesting, I've honestly never actually used this logic in this exact context, so I'm glad you pointed that out. Thanks. – Steve Danner Aug 26 '10 at 00:33