【问题标题】:Test if a property is an ICollection in Portable class library测试一个属性是否是可移植类库中的 ICollection
【发布时间】:2016-09-09 18:54:57
【问题描述】:

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

这是我的方法:

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?

有什么我可以做的吗?

【问题讨论】:

标签: c# reflection portable-class-library


【解决方案1】:

我通过 linq Linq 解决了它排除这种类型

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2012-10-14
    相关资源
    最近更新 更多