忽略PropertyInfo中的集合属性

时间:2010-05-14 14:02:21

标签: c# properties propertyinfo

我有一个使用此代码的函数:

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;

但是,它不喜欢我!

哪种方法更好?

3 个答案:

答案 0 :(得分:16)

我在想你可能想检查属性实现类型的接口。 (删除了冗余接口,因为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);
    }
}

答案 1 :(得分:9)

我可能会查看IEnumerable

if ((typeof(string) != propertyInfo.PropertyType) 
    && typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
{
    continue;
}

答案 2 :(得分:5)

bool isCollection = typeof(System.Collections.IEnumerable)
                          .IsAssignableFrom(propertyInfo.PropertyType);