测试属性是否为可移植类库中的ICollection



我试图测试我的对象的属性是否为集合,但遇到问题,因为在可移植类库中许多方法不可用

这是我的方法:

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)
    { ...

现在我必须测试字段是否是一个集合,但是

  • implements interfaces不可用
  • IsAssignableFrom不可用
  • GetInterfaces()不可用
  • GetGenericTypeDefinition()不可用

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

我能做点什么吗?

我通过linq解决了这个问题,linq

IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields.Where(x => x.FieldType.Name != typeof(ICollection<>).Name); 

最新更新