Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not check object references for equality after hot reload #73009

Merged
merged 20 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Globalization;
using System.Reflection.Metadata;
using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;

namespace System.Reflection
Expand Down Expand Up @@ -33,8 +34,7 @@ internal override bool CacheEquals(object? o)
return
o is MdFieldInfo m &&
m.m_tkField == m_tkField &&
m_declaringType.TypeHandle.GetModuleHandle().Equals(
m.m_declaringType.TypeHandle.GetModuleHandle());
ReferenceEquals(m_declaringType, m.m_declaringType);
}
#endregion

Expand All @@ -43,6 +43,13 @@ o is MdFieldInfo m &&

public override int MetadataToken => m_tkField;
internal override RuntimeModule GetRuntimeModule() { return m_declaringType.GetRuntimeModule(); }

public override bool Equals(object? obj) =>
ReferenceEquals(this, obj) ||
(MetadataUpdater.IsSupported && CacheEquals(obj));

public override int GetHashCode() =>
HashCode.Combine(m_tkField.GetHashCode(), m_declaringType.GetUnderlyingNativeHandle().GetHashCode());
#endregion

#region FieldInfo Overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Globalization;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;

Expand Down Expand Up @@ -183,6 +184,13 @@ internal override RuntimeModule GetRuntimeModule()
return RuntimeTypeHandle.GetModule(RuntimeFieldHandle.GetApproxDeclaringType(this));
}

public override bool Equals(object? obj) =>
ReferenceEquals(this, obj) ||
(MetadataUpdater.IsSupported && CacheEquals(obj));

public override int GetHashCode() =>
HashCode.Combine(m_fieldHandle.GetHashCode(), m_declaringType.GetUnderlyingNativeHandle().GetHashCode());

#endregion

#region FieldInfo Overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -72,7 +73,8 @@ internal RuntimeConstructorInfo(
RuntimeMethodHandleInternal IRuntimeMethodInfo.Value => new RuntimeMethodHandleInternal(m_handle);

internal override bool CacheEquals(object? o) =>
o is RuntimeConstructorInfo m && m.m_handle == m_handle;
o is RuntimeConstructorInfo m && m.m_handle == m_handle &&
ReferenceEquals(m_declaringType, m.m_declaringType);

internal Signature Signature
{
Expand Down Expand Up @@ -118,6 +120,13 @@ public override string ToString()

return m_toString;
}

public override bool Equals(object? obj) =>
ReferenceEquals(this, obj) ||
(MetadataUpdater.IsSupported && CacheEquals(obj));

public override int GetHashCode() =>
HashCode.Combine(m_handle.GetHashCode(), m_declaringType.GetUnderlyingNativeHandle().GetHashCode());
#endregion

#region ICustomAttributeProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.Metadata;
using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;

namespace System.Reflection
Expand Down Expand Up @@ -53,8 +54,7 @@ internal override bool CacheEquals(object? o)
return
o is RuntimeEventInfo m &&
m.m_token == m_token &&
RuntimeTypeHandle.GetModule(m_declaringType).Equals(
RuntimeTypeHandle.GetModule(m.m_declaringType));
ReferenceEquals(m_declaringType, m.m_declaringType);
}

internal BindingFlags BindingFlags => m_bindingFlags;
Expand All @@ -68,6 +68,13 @@ public override string ToString()

return m_addMethod.GetParametersNoCopy()[0].ParameterType.FormatTypeName() + " " + Name;
}

public override bool Equals(object? obj) =>
ReferenceEquals(this, obj) ||
(MetadataUpdater.IsSupported && CacheEquals(obj));

public override int GetHashCode() =>
HashCode.Combine(m_token.GetHashCode(), m_declaringType.GetUnderlyingNativeHandle().GetHashCode());
#endregion

#region ICustomAttributeProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal RuntimeType GetDeclaringTypeInternal()

public override Module Module => GetRuntimeModule();
public override bool IsCollectible => m_declaringType.IsCollectible;

#endregion

#region Object Overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,56 +157,18 @@ public override string ToString()
return m_toString;
}

public override int GetHashCode()
{
// See RuntimeMethodInfo.Equals() below.
if (IsGenericMethod)
return RuntimeHelpers.GetHashCodeOfPtr(m_handle);
else
return base.GetHashCode();
}

public override bool Equals(object? obj)
{
if (!IsGenericMethod)
return obj == (object)this;

// We cannot do simple object identity comparisons for generic methods.
// Equals will be called in CerHashTable when RuntimeType+RuntimeTypeCache.GetGenericMethodInfo()
// retrieve items from and insert items into s_methodInstantiations which is a CerHashtable.

RuntimeMethodInfo? mi = obj as RuntimeMethodInfo;

if (mi == null || !mi.IsGenericMethod)
return false;

// now we know that both operands are generic methods

IRuntimeMethodInfo handle1 = RuntimeMethodHandle.StripMethodInstantiation(this);
IRuntimeMethodInfo handle2 = RuntimeMethodHandle.StripMethodInstantiation(mi);
if (handle1.Value.Value != handle2.Value.Value)
return false;

Type[] lhs = GetGenericArguments();
Type[] rhs = mi.GetGenericArguments();
// We cannot do simple object identity comparisons due to generic methods.
// Equals and GetHashCode will be called in CerHashTable when RuntimeType+RuntimeTypeCache.GetGenericMethodInfo()
// retrieve items from and insert items into s_methodInstantiations.

if (lhs.Length != rhs.Length)
return false;
public override int GetHashCode() =>
HashCode.Combine(m_handle.GetHashCode(), m_declaringType.GetUnderlyingNativeHandle().GetHashCode());

for (int i = 0; i < lhs.Length; i++)
{
if (lhs[i] != rhs[i])
return false;
}
public override bool Equals(object? obj) =>
obj is RuntimeMethodInfo m && m_handle == m.m_handle &&
ReferenceEquals(m_declaringType, m.m_declaringType) &&
ReferenceEquals(m_reflectedTypeCache.GetRuntimeType(), m.m_reflectedTypeCache.GetRuntimeType());

if (DeclaringType != mi.DeclaringType)
return false;

if (ReflectedType != mi.ReflectedType)
return false;

return true;
}
#endregion

#region ICustomAttributeProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection.Metadata;
using System.Text;
using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;

Expand Down Expand Up @@ -55,8 +56,7 @@ internal override bool CacheEquals(object? o)
return
o is RuntimePropertyInfo m &&
m.m_token == m_token &&
RuntimeTypeHandle.GetModule(m_declaringType).Equals(
RuntimeTypeHandle.GetModule(m.m_declaringType));
ReferenceEquals(m_declaringType, m.m_declaringType);
}

internal Signature Signature
Expand Down Expand Up @@ -179,6 +179,13 @@ public override IList<CustomAttributeData> GetCustomAttributesData()
public override Module Module => GetRuntimeModule();
internal RuntimeModule GetRuntimeModule() { return m_declaringType.GetRuntimeModule(); }
public override bool IsCollectible => m_declaringType.IsCollectible;

public override bool Equals(object? obj) =>
ReferenceEquals(this, obj) ||
(MetadataUpdater.IsSupported && CacheEquals(obj));

public override int GetHashCode() =>
HashCode.Combine(m_token.GetHashCode(), m_declaringType.GetUnderlyingNativeHandle().GetHashCode());
#endregion

#region PropertyInfo Overrides
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Runtime/tests/System/DelegateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public static void SameGenericMethodObtainedViaDelegateAndReflectionAreSameForCl
var m1 = ((MethodCallExpression)((Expression<Action>)(() => new ClassG().M<string, object>())).Body).Method;
var m2 = new Action(new ClassG().M<string, object>).Method;
Assert.True(m1.Equals(m2));
Assert.True(m1.GetHashCode().Equals(m2.GetHashCode()));
Assert.Equal(m1.GetHashCode(), m2.GetHashCode());
Assert.Equal(m1.MethodHandle.Value, m2.MethodHandle.Value);
}

Expand All @@ -468,7 +468,7 @@ public static void SameGenericMethodObtainedViaDelegateAndReflectionAreSameForSt
var m1 = ((MethodCallExpression)((Expression<Action>)(() => new StructG().M<string, object>())).Body).Method;
var m2 = new Action(new StructG().M<string, object>).Method;
Assert.True(m1.Equals(m2));
Assert.True(m1.GetHashCode().Equals(m2.GetHashCode()));
Assert.Equal(m1.GetHashCode(), m2.GetHashCode());
Assert.Equal(m1.MethodHandle.Value, m2.MethodHandle.Value);
}

Expand Down
Loading