Skip to content

Commit 5397399

Browse files
github-actions[bot]buyaa-nsteveharter
authored
[release/7.0] Fix Equals in hot reload scenario (#78258)
* Fix Equals in hot reload scenario * Update src/libraries/System.Runtime/tests/System/Reflection/ReflectionCacheTests.cs Co-authored-by: Steve Harter <[email protected]> Co-authored-by: Buyaa Namnan <[email protected]> Co-authored-by: Buyaa Namnan <[email protected]> Co-authored-by: Steve Harter <[email protected]>
1 parent 1c341de commit 5397399

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/MdFieldInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ o is MdFieldInfo m &&
4646

4747
public override bool Equals(object? obj) =>
4848
ReferenceEquals(this, obj) ||
49-
(MetadataUpdater.IsSupported && CacheEquals(obj));
49+
(MetadataUpdater.IsSupported &&
50+
obj is MdFieldInfo fi &&
51+
fi.m_tkField == m_tkField &&
52+
ReferenceEquals(fi.m_declaringType, m_declaringType) &&
53+
ReferenceEquals(fi.m_reflectedTypeCache.GetRuntimeType(), m_reflectedTypeCache.GetRuntimeType()));
5054

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

src/coreclr/System.Private.CoreLib/src/System/Reflection/RtFieldInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ internal override RuntimeModule GetRuntimeModule()
118118

119119
public override bool Equals(object? obj) =>
120120
ReferenceEquals(this, obj) ||
121-
(MetadataUpdater.IsSupported && CacheEquals(obj));
121+
(MetadataUpdater.IsSupported &&
122+
obj is RtFieldInfo fi &&
123+
fi.m_fieldHandle == m_fieldHandle &&
124+
ReferenceEquals(fi.m_reflectedTypeCache.GetRuntimeType(), m_reflectedTypeCache.GetRuntimeType()));
122125

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

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ public override string ToString()
7171

7272
public override bool Equals(object? obj) =>
7373
ReferenceEquals(this, obj) ||
74-
(MetadataUpdater.IsSupported && CacheEquals(obj));
74+
(MetadataUpdater.IsSupported &&
75+
obj is RuntimeEventInfo ei &&
76+
ei.m_token == m_token &&
77+
ReferenceEquals(ei.m_declaringType, m_declaringType) &&
78+
ReferenceEquals(ei.m_reflectedTypeCache.GetRuntimeType(), m_reflectedTypeCache.GetRuntimeType()));
7579

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

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ public override IList<CustomAttributeData> GetCustomAttributesData()
182182

183183
public override bool Equals(object? obj) =>
184184
ReferenceEquals(this, obj) ||
185-
(MetadataUpdater.IsSupported && CacheEquals(obj));
185+
(MetadataUpdater.IsSupported && obj is RuntimePropertyInfo rpi &&
186+
rpi.m_token == m_token &&
187+
ReferenceEquals(rpi.m_declaringType, m_declaringType) &&
188+
ReferenceEquals(rpi.m_reflectedTypeCache.GetRuntimeType(), m_reflectedTypeCache.GetRuntimeType()));
186189

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

src/libraries/System.Runtime/tests/System/Reflection/ReflectionCacheTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@
77

88
namespace System.Reflection.Tests
99
{
10+
public class A
11+
{
12+
public string P { get; set; }
13+
public int F;
14+
#pragma warning disable CS0067
15+
public event EventHandler E;
16+
#pragma warning restore CS0067
17+
public void M() { }
18+
}
19+
1020
[Collection(nameof(DisableParallelization))]
11-
public class ReflectionCacheTests
21+
public class ReflectionCacheTests : A
1222
{
1323
private static bool IsMetadataUpdateAndRemoteExecutorSupported => PlatformDetection.IsMetadataUpdateSupported && RemoteExecutor.IsSupported;
1424

@@ -47,6 +57,12 @@ public void GetMembers_MultipleCalls_SameObjects()
4757
AssertSameEqualAndHashCodeEqual(fi1, fi2);
4858
AssertSameEqualAndHashCodeEqual(ei1, ei2);
4959
AssertSameEqualAndHashCodeEqual(ci1, ci2);
60+
61+
PropertyInfo parentProperty = typeof(A).GetProperty("P");
62+
PropertyInfo childProperty = s_type.GetProperty("P");
63+
Assert.NotNull(parentProperty);
64+
Assert.NotNull(childProperty);
65+
Assert.NotEqual(parentProperty, childProperty);
5066
}
5167

5268
void AssertSameEqualAndHashCodeEqual(object o1, object o2)
@@ -87,6 +103,20 @@ public void GetMembers_MultipleCalls_ClearCache_ReflectionCacheTestsType()
87103
EventInfo ei1 = s_type.GetEvent(nameof(Event1));
88104
ConstructorInfo ci1 = s_type.GetConstructor(Type.EmptyTypes);
89105

106+
PropertyInfo parentProperty = typeof(A).GetProperty("P");
107+
PropertyInfo childProperty = s_type.GetProperty("P");
108+
FieldInfo parentField = typeof(A).GetField("F");
109+
FieldInfo childField = s_type.GetField("F");
110+
MethodInfo parentMethod = typeof(A).GetMethod("M");
111+
MethodInfo childMethod = s_type.GetMethod("M");
112+
EventInfo parentEvent = typeof(A).GetEvent("E");
113+
EventInfo childEvent = s_type.GetEvent("E");
114+
115+
Assert.NotEqual(parentProperty, childProperty);
116+
Assert.NotEqual(parentField, childField);
117+
Assert.NotEqual(parentMethod, childMethod);
118+
Assert.NotEqual(parentEvent, childEvent);
119+
90120
clearCache(new[] { typeof(ReflectionCacheTests) });
91121

92122
MethodInfo mi2 = s_type.GetMethod(nameof(Method));
@@ -95,6 +125,11 @@ public void GetMembers_MultipleCalls_ClearCache_ReflectionCacheTestsType()
95125
EventInfo ei2 = s_type.GetEvent(nameof(Event1));
96126
ConstructorInfo ci2 = s_type.GetConstructor(Type.EmptyTypes);
97127

128+
Assert.NotEqual(parentProperty, childProperty);
129+
Assert.NotEqual(parentField, childField);
130+
Assert.NotEqual(parentMethod, childMethod);
131+
Assert.NotEqual(parentEvent, childEvent);
132+
98133
AssertNotSameSameButEqualAndHashCodeEqual(mi1, mi2);
99134
AssertNotSameSameButEqualAndHashCodeEqual(pi1, pi2);
100135
AssertNotSameSameButEqualAndHashCodeEqual(fi1, fi2);

0 commit comments

Comments
 (0)