Below is the code that I use to get the initial state of all public properties in the class to check for IsDirty.
What is the easiest way to see if an IEnumerable property is?
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 did was add a few library extensions, as shown below.
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); }
reflection c #
Berryl Aug 25 '10 at 20:08 2010-08-25 20:08
source share