views:

97

answers:

1

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> ????? )

}
+3  A: 

GetGenericTypeDefinition and typeof(Collection<>) will do the job:

if(typeof(Collection<>).IsAssignableFrom(p.GetType().GetGenericTypeDefinition())
Anton Gogolev
Shouldn't you test against something like `ICollection<T>` rather than `Collection<T>`? Many of the generic collections (eg, `List<T>`) don't inherit from `Collection<T>`.
LukeH