I am trying to extract all properties ICollection<T>from a class of an unknown type. Also, the type T (what the collection has) is unknown at compile time. First, I tried this approach:
foreach (var property in entity.GetType().GetProperties())
{
if (typeof(ICollection).IsAssignableFrom(property.PropertyType) || typeof(ICollection<>).IsAssignableFrom(property.PropertyType))
{
}
}
but it does not work (false value even for properties ICollection).
I started working like this:
foreach (var property in entity.GetType().GetProperties())
{
var getMethod = property.GetGetMethod();
var test = getMethod.Invoke(entity, null);
if (test is ICollection)
{
}
}
but I do not want to do all getters. Why does the first part of the code not work? How to find properties ICollectionwithout executing all getters?
source
share