让我们看看编译后的程序集的元数据,以确保这两个属性除了名称之外具有相同的结构:
我使用 ILDASM 而不是通常的反编译器工具来确保没有任何内容被隐藏或以更友好的方式显示。除了名称之外,这两个属性是相同的。
返回的两个Prop1 属性之一来自Class1,其中一个来自Class2。
这似乎是一个错误。该错误似乎是基类成员未正确添加到结果中。当DeclaredOnly 未指定时all inherited properties should be returned as well。
我正在使用 DotPeek 和 Reflector VS 扩展,它允许调试反编译的 BCL 代码来调试反射代码。这个问题中看到的行为是在这个方法中触发的:
private void PopulateProperties(RuntimeType.RuntimeTypeCache.Filter filter, RuntimeType declaringType, Dictionary<string, List<RuntimePropertyInfo>> csPropertyInfos, bool[] usedSlots, ref RuntimeType.ListBuilder<RuntimePropertyInfo> list)
{
int token = RuntimeTypeHandle.GetToken(declaringType);
if (MetadataToken.IsNullToken(token))
return;
MetadataEnumResult result;
RuntimeTypeHandle.GetMetadataImport(declaringType).EnumProperties(token, out result);
RuntimeModule module = RuntimeTypeHandle.GetModule(declaringType);
int numVirtuals = RuntimeTypeHandle.GetNumVirtuals(declaringType);
for (int index1 = 0; index1 < result.Length; ++index1)
{
int num = result[index1];
if (filter.RequiresStringComparison())
{
if (ModuleHandle.ContainsPropertyMatchingHash(module, num, filter.GetHashToMatch()))
{
Utf8String name = declaringType.GetRuntimeModule().MetadataImport.GetName(num);
if (!filter.Match(name))
continue;
}
else
continue;
}
bool isPrivate;
RuntimePropertyInfo runtimePropertyInfo = new RuntimePropertyInfo(num, declaringType, this.m_runtimeTypeCache, out isPrivate);
if (usedSlots != null)
{
if (!(declaringType != this.ReflectedType) || !isPrivate)
{
MethodInfo methodInfo = runtimePropertyInfo.GetGetMethod();
if (methodInfo == (MethodInfo) null)
methodInfo = runtimePropertyInfo.GetSetMethod();
if (methodInfo != (MethodInfo) null)
{
int slot = RuntimeMethodHandle.GetSlot((IRuntimeMethodInfo) methodInfo);
if (slot < numVirtuals)
{
if (!usedSlots[slot])
usedSlots[slot] = true;
else
continue;
}
}
if (csPropertyInfos != null)
{
string name = runtimePropertyInfo.Name;
List<RuntimePropertyInfo> list1 = csPropertyInfos.GetValueOrDefault(name);
if (list1 == null)
{
list1 = new List<RuntimePropertyInfo>(1);
csPropertyInfos[name] = list1;
}
for (int index2 = 0; index2 < list1.Count; ++index2)
{
if (runtimePropertyInfo.EqualsSig(list1[index2]))
{
list1 = (List<RuntimePropertyInfo>) null;
break;
}
}
if (list1 != null)
list1.Add(runtimePropertyInfo);
else
continue;
}
else
{
bool flag = false;
for (int index2 = 0; index2 < list.Count; ++index2)
{
if (runtimePropertyInfo.EqualsSig(list[index2]))
{
flag = true;
break;
}
}
if (flag)
continue;
}
}
else
continue;
}
list.Add(runtimePropertyInfo);
}
}
为什么公共属性的行为会消失?
if (!(declaringType != this.ReflectedType) || !isPrivate)
有一个检查。
Class1<string>.Prop2 在这里被过滤掉了:
bool flag = false;
for (int index2 = 0; index2 < list.Count; ++index2)
{
if (runtimePropertyInfo.EqualsSig(list[index2]))
{
flag = true;
break;
}
}
if (flag)
continue;
因为EqualsSig 返回真。如果您要求私人成员,似乎属性会按名称和 sig 进行重复数据删除......我不知道为什么。不过似乎是故意的。
遵循这个复杂的代码很累。 This is better and commented. 我怀疑他们正在删除私有属性,因为您可以通过从某个类继承来提升权限以获取它的所有私有成员。
答案如下:
// For backward compatibility, even if the vtable slots don't match, we will still treat
// a property as duplicate if the names and signatures match.
因此他们添加了一个 hack 以实现向后兼容性。
您必须添加自己的处理才能获得所需的行为。也许,Fastreflect 可以提供帮助。