我怎么知道某个属性是否是通用集合 [英] How would I know if a property is a generic collection

查看:90
本文介绍了我怎么知道某个属性是否是通用集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道使用PropertyInfo类的类中属性的类型是否是通用集合(List,ObservableCollection).

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 in (o.GetType()).GetProperties())
{
    if(p is Collection<T> ????? )

}

推荐答案

GetGenericTypeDefinitiontypeof(Collection<>)将完成工作:

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

这篇关于我怎么知道某个属性是否是通用集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆