.NET PropertyInfos的平等性

Cha*_*ion 7 .net c# reflection propertyinfo

我有一些代码将2个PropertyInfos与Equals()进行比较.虽然这通常似乎有效,但我遇到了一个奇怪的情况,即同一底层属性的两个反射属性信息对象不相等:

PropertyInfo prop1, prop2; // both are public and not static
Console.WriteLine(prop1 == prop2); // false ???
Console.WriteLine(Equals(prop1, prop2)); // false ???
Console.WriteLine(prop1.DeclaringType == prop2.DeclaringType); // true
Console.WriteLine(prop1.ReturnType == prop2.ReturnType); // true
Console.WriteLine(prop1.Name == prop2.Name); // true
Console.WriteLine(prop1.DeclaringType.GetProperties().Contains(prop1)); // true
Console.WriteLine(prop2.DeclaringType.GetProperties().Contains(prop2)); // false ???
Run Code Online (Sandbox Code Playgroud)

看起来PropertyInfo实际上并没有实现Equals(),但我认为.NET缓存反映了成员,因此总是返回相同的实例.你当然总是看到a.GetType()== b.GetType().PropertyInfos的情况不是这样吗?

其他一些注意事项: - 在.NET 4,VS2012,x86构建目标中运行NUnit测试时发生了这种奇怪现象 - 这种情况甚至不会发生在我们用这种方式进行比较的所有属性上,但它在一个属性上一致地失败.

谁能解释这种行为?

编辑:如果有人有兴趣,这里是我写的比较MemberInfos的EqualityComparison函数:

public class MemberEqualityComparer : EqualityComparer<MemberInfo> {
    public override bool Equals(MemberInfo @this, MemberInfo that) {
        if (@this == that) { return true; }
        if (@this == null || that == null) { return false; }

                        // handles everything except for generics
                    if (@this.MetadataToken != that.MetadataToken
                        || !Equals(@this.Module, that.Module)
                        || this.Equals(@this.DeclaringType, that.DeclaringType))
                    {
                        return false;
                    }

                    bool areEqual;
                    switch (@this.MemberType)
                    {
                        // constructors and methods can be generic independent of their types,
                        // so they are equal if they're generic arguments are equal
                        case MemberTypes.Constructor:
                        case MemberTypes.Method:
                            var thisMethod = @this as MethodBase;
                            var thatMethod = that as MethodBase;
                                                areEqual = thisMethod.GetGenericArguments().SequenceEqual(thatMethod.GetGenericArguments(), 
this);
                            break;
                        // properties, events, and fields cannot be generic independent of their types,
                        // so if we've reached this point without bailing out we just return true.
                        case MemberTypes.Property:
                        case MemberTypes.Event:
                        case MemberTypes.Field:
                            areEqual = true;
                            break;
                        // the system guarantees reference equality for types, so if we've reached this point
                        // without returning true the two are not equal
                        case MemberTypes.TypeInfo:
                        case MemberTypes.NestedType:
                            areEqual = false;
                            break;
                        default:
                            throw new NotImplementedException(@this.MemberType.ToString());
    }

    public override int GetHashCode(MemberInfo memberInfo) {
        if (memberInfo == null) { return 0; }

    var hash = @this.MetadataToken 
        ^ @this.Module.GetHashCode() 
        ^ this.GetHashCode(@this.DeclaringType);
    return hash;
    }
}
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 13

仅为Type类承诺对象标识,而不为其他反射类承诺.一个可能的平等,比较合理的方式是检查的属性具有相同的元数据标记,并从同一模块来了.试试这个:

bool equal = prop1.MetadataToken == prop2.MetadataToken &&
             prop1.Module.Equals(prop2.Module);
Run Code Online (Sandbox Code Playgroud)

只要ecma 335适用,这是有道理的.我没有对你的代码进行测试,因为你没有发布它.所以试试吧.


Mar*_*ell 6

我猜他们有不同ReflectedType.例如,继承:

class A {
   public int Foo {get;set;}
}
class B : A {}
Run Code Online (Sandbox Code Playgroud)

现在看看typeof(A).GetProperty("Foo")typeof(B).GetProperty("Foo").