Aşağıda, IsDirty denetimi için bir sınıftaki tüm genel özelliklerin başlangıç durumunu almak için kullanacağım bir kod var.PropertyInfo ürününün koleksiyon olup olmadığını nasıl öğrenebilirim?
Bir özelliğin IEnumerable olup olmadığını görmenin en kolay yolu nedir?
Alkış,
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);
}
GÜNCELLEME
ben şöyle bir kaç kütüphane uzantıları ekleyerek edildi oynamak zorunda kalırdı ne
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);
}
Not çok –
o Geçenlerde x.IsSubClassOf (y) 'yanlış ise x == dönecektir' fark http://stackoverflow.com/a/40376537/5996253 –