How would I know if a property is a generic collection

前端 未结 2 987
借酒劲吻你
借酒劲吻你 2021-02-13 13:24

I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class.

foreach (PropertyInfo p          


        
2条回答
  •  遥遥无期
    2021-02-13 14:25

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

    if(p.PropertyType.IsGenericType && typeof(Collection<>).IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())
    

提交回复
热议问题