Равенство для .NET PropertyInfos

У меня есть код, который сравнивает 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 ???

Похоже, что PropertyInfo на самом деле не реализует Equals (), но я подумал, что .NET кеширует отраженные элементы, так что всегда возвращается один и тот же экземпляр. Вы наверняка видите a.GetType () == b.GetType () все время. Разве это не относится к PropertyInfos?

Некоторые другие замечания: -Эта странность произошла при запуске теста NUnit в .NET 4, VS2012, x86 build build -Это не 'Это может произойти даже для всех свойств, которые мы сравниваем таким образом, но в одном свойстве это не выполняется.

Кто-нибудь может объяснить это поведение?

РЕДАКТИРОВАТЬ: в случае, если кто-то заинтересован, вот функция EqualityComparison, которую я написал, чтобы сравнить MemberInfos:

public class MemberEqualityComparer : EqualityComparer {
    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;
    }
}

Ответы на вопрос(2)

Ваш ответ на вопрос