Unten ist ein Code, den ich verwende, um den Anfangsstatus aller öffentlichen Eigenschaften in einer Klasse für IsDirty zu überprüfen.Wie Sie wissen, ob eine PropertyInfo eine Sammlung ist
Was ist der einfachste Weg zu sehen, ob eine Eigenschaft IEnumerable ist?
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
Was ich ein paar Bibliothekserweiterungen tun liquidiert wurde die Zugabe als
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);
}
Beachten Sie, dass ein String eine IEnumerable ist zu –
Sie löst das Problem http://stackoverflow.com/a/40376537/5996253 –