加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 忽略PropertyInfo中的集合属性

发布时间:2020-12-15 06:42:23 所属栏目:百科 来源:网络整理
导读:我有一个这个代码的功能: foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()){//SOME CODEif (propertyInfo.CanWrite) propertyInfo.SetValue(myCopy,propertyInfo.GetValue(obj,null),null);} 我会避免检查“收集”属性;这样做现在我已经
我有一个这个代码的功能:
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()){
//SOME CODE
if (propertyInfo.CanWrite)
    propertyInfo.SetValue(myCopy,propertyInfo.GetValue(obj,null),null);
}

我会避免检查“收集”属性;这样做现在我已经插入了这个控件:

if (propertyInfo.PropertyType.Name.Contains("List")
     || propertyInfo.PropertyType.Name.Contains("Enumerable")
     || propertyInfo.PropertyType.Name.Contains("Collection"))
     continue;

但是,它不喜欢我!

哪一种更好的方法呢?

解决方法

我在想你可能想要检查接口的属性实现的类型. (删除冗余接口,IList继承ICollection和ICollection继承IEnumerable.)
static void DoSomething<T>()
{
    List<Type> collections = new List<Type>() { typeof(IEnumerable<>),typeof(IEnumerable) };

    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
    {
        if (propertyInfo.PropertyType != typeof(string) && propertyInfo.PropertyType.GetInterfaces().Any(i => collections.Any(c => i == c)))
        {
            continue;
        }

        Console.WriteLine(propertyInfo.Name);
    }
}

我添加了不拒绝字符串的代码,因为它也实现了IEnumerable,我想你可能想要保留它们.

鉴于先前的收集接口列表的冗余,只需编写这样的代码就可能更简单

static void DoSomething<T>()
{
    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
    {
        if (propertyInfo.PropertyType != typeof(string)
            && propertyInfo.PropertyType.GetInterface(typeof(IEnumerable).Name) != null
            && propertyInfo.PropertyType.GetInterface(typeof(IEnumerable<>).Name) != null)
        {
            continue;
        }

        Console.WriteLine(propertyInfo.Name);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读