【发布时间】: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?
有什么我可以做的吗?
【问题讨论】:
-
有一个用于 PCL 的新反射 API。请参阅此问题的答案stackoverflow.com/questions/14061291/… 阅读博文。
-
我读过 blogs.msdn.microsoft.com/dotnet/2012/08/28/… 实际上我将它用于 DeclaredFields 但现在看不到如何获得我需要的东西
标签: c# reflection portable-class-library