diff --git a/src/mscorlib/shared/System/Reflection/MemberInfo.cs b/src/mscorlib/shared/System/Reflection/MemberInfo.cs index 1275cc15a054..d8a7458632b4 100644 --- a/src/mscorlib/shared/System/Reflection/MemberInfo.cs +++ b/src/mscorlib/shared/System/Reflection/MemberInfo.cs @@ -30,6 +30,8 @@ public virtual Module Module } } + public virtual bool HasSameMetadataDefinitionAs(MemberInfo other) { throw NotImplemented.ByDesign; } + public abstract bool IsDefined(Type attributeType, bool inherit); public abstract object[] GetCustomAttributes(bool inherit); public abstract object[] GetCustomAttributes(Type attributeType, bool inherit); diff --git a/src/mscorlib/src/System/Reflection/MemberInfo.Internal.cs b/src/mscorlib/src/System/Reflection/MemberInfo.Internal.cs index 8e7be565114b..9bb7b5741320 100644 --- a/src/mscorlib/src/System/Reflection/MemberInfo.Internal.cs +++ b/src/mscorlib/src/System/Reflection/MemberInfo.Internal.cs @@ -7,5 +7,23 @@ namespace System.Reflection public abstract partial class MemberInfo { internal virtual bool CacheEquals(object o) { throw new NotImplementedException(); } + + internal bool HasSameMetadataDefinitionAsCore(MemberInfo other) where TOther : MemberInfo + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + // Ensure that "other" is a runtime-implemented MemberInfo. Do this check before calling any methods on it! + if (!(other is TOther)) + return false; + + if (MetadataToken != other.MetadataToken) + return false; + + if (!(Module.Equals(other.Module))) + return false; + + return true; + } } } diff --git a/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs b/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs index 8c3b1fce98e1..96930815e2c1 100644 --- a/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs +++ b/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs @@ -218,6 +218,8 @@ public override Type DeclaringType } } + public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); + public override Type ReflectedType { get diff --git a/src/mscorlib/src/System/Reflection/RuntimeEventInfo.cs b/src/mscorlib/src/System/Reflection/RuntimeEventInfo.cs index 930e1820bdf5..88f1aff32d36 100644 --- a/src/mscorlib/src/System/Reflection/RuntimeEventInfo.cs +++ b/src/mscorlib/src/System/Reflection/RuntimeEventInfo.cs @@ -135,6 +135,7 @@ public override String Name } } public override Type DeclaringType { get { return m_declaringType; } } + public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); public override Type ReflectedType { get diff --git a/src/mscorlib/src/System/Reflection/RuntimeFieldInfo.cs b/src/mscorlib/src/System/Reflection/RuntimeFieldInfo.cs index 29cc97d2252c..b3dceaa1e2cb 100644 --- a/src/mscorlib/src/System/Reflection/RuntimeFieldInfo.cs +++ b/src/mscorlib/src/System/Reflection/RuntimeFieldInfo.cs @@ -68,6 +68,8 @@ public override Type DeclaringType } } + public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); + public override Module Module { get { return GetRuntimeModule(); } } #endregion diff --git a/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs b/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs index b8a2341e4e85..9a7da4d40815 100644 --- a/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs +++ b/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs @@ -335,6 +335,8 @@ public override Type DeclaringType } } + public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); + public override Type ReflectedType { get diff --git a/src/mscorlib/src/System/Reflection/RuntimePropertyInfo.cs b/src/mscorlib/src/System/Reflection/RuntimePropertyInfo.cs index b6a4792e4f8c..d684e6c8e93d 100644 --- a/src/mscorlib/src/System/Reflection/RuntimePropertyInfo.cs +++ b/src/mscorlib/src/System/Reflection/RuntimePropertyInfo.cs @@ -207,6 +207,8 @@ public override Type DeclaringType } } + public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); + public override Type ReflectedType { get diff --git a/src/mscorlib/src/System/RtType.cs b/src/mscorlib/src/System/RtType.cs index e49894f3455b..4bf78b239640 100644 --- a/src/mscorlib/src/System/RtType.cs +++ b/src/mscorlib/src/System/RtType.cs @@ -3789,6 +3789,8 @@ public override Type[] GetGenericParameterConstraints() #endregion #region Misc + public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); + public override bool IsTypeDefinition { get { return RuntimeTypeHandle.IsTypeDefinition(this); }