Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active July 10, 2020 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsscoder/855f4d4bce0190f9b2f7ed70a4787d67 to your computer and use it in GitHub Desktop.
Save gsscoder/855f4d4bce0190f9b2f7ed70a4787d67 to your computer and use it in GitHub Desktop.
C# extension method to discover if a property is a collection
using System.Collections.Generic;
using System.Reflection;
public static class PropertyInfoExtensions
{
public static bool IsEnumerable(this PropertyInfo property) =>
(property.PropertyType.IsGenericType &&
property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment