测试属性是否是Portable类库中的ICollection

时间:2016-08-29 12:56:46

标签: c# reflection portable-class-library

我试图测试我的对象的属性是否是一个集合但是遇到问题,因为在便携式类库中很多方法都是不可用的

这是我的方法:

public virtual bool Equals(TObject other) // where TObject : class
{
    if (other == null)
        return false;

    Type t = GetType();
    Type otherType = other.GetType();

    TypeInfo typeInfo = t.GetTypeInfo();            

    if (t != otherType)
        return false;

    IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields; 
    foreach (FieldInfo field in fields)
    { ...

现在我必须测试字段是否是Icollection而不是

  • ImplementedInterfaces无法使用
  • IsAssignableFrom无法使用
  • GetInterfaces()无法使用
  • GetGenericTypeDefinition()不可用

也许这是因为我有FieldInfo而不是TypeInfo?

有什么我可以做的吗?

1 个答案:

答案 0 :(得分:0)

我通过linq Linq

解决了这种类型的问题
IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields.Where(x => x.FieldType.Name != typeof(ICollection<>).Name);