我试图测试我的对象的属性是否是一个集合但是遇到问题,因为在便携式类库中很多方法都是不可用的
这是我的方法:
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而不是
也许这是因为我有FieldInfo而不是TypeInfo?
有什么我可以做的吗?
答案 0 :(得分:0)
我通过linq Linq
解决了这种类型的问题IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields.Where(x => x.FieldType.Name != typeof(ICollection<>).Name);