I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class.
enter code here
foreach (PropertyInfo p in (o.GetType()).GetProperties())
{
if(p is Collection<T> ????? )
}
I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class.
enter code here
foreach (PropertyInfo p in (o.GetType()).GetProperties())
{
if(p is Collection<T> ????? )
}
GetGenericTypeDefinition
and typeof(Collection<>)
will do the job:
if(typeof(Collection<>).IsAssignableFrom(p.GetType().GetGenericTypeDefinition())