Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -11,11 +11,16 @@ namespace System.Reflection.Metadata
/// <summary>Metadata update handler used to clear a Type's reflection cache in response to a metadata update notification.</summary>
internal static class RuntimeTypeMetadataUpdateHandler
{
private static bool s_cacheCleared;
public static bool MetadataUpdaterSupportedAndCacheCleared => MetadataUpdater.IsSupported && s_cacheCleared;

/// <summary>Clear type caches in response to an update notification.</summary>
/// <param name="types">The specific types to be cleared, or null to clear everything.</param>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Clearing the caches on a Type isn't affected if a Type is trimmed, or has any of its members trimmed.")]
public static void ClearCache(Type[]? types)
{
s_cacheCleared = true;

if (RequiresClearingAllTypes(types))
{
// TODO: This should ideally be in a QCall in the runtime. As written here:
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 @@ -118,6 +119,15 @@ public override string ToString()

return m_toString;
}

public override bool Equals(object? obj) =>
obj == (object)this ||
(RuntimeTypeMetadataUpdateHandler.MetadataUpdaterSupportedAndCacheCleared &&
obj is RuntimeConstructorInfo m &&
MetadataToken == m.MetadataToken &&
RuntimeTypeHandle.GetModule(m_declaringType).Equals(RuntimeTypeHandle.GetModule(m.m_declaringType)));

public override int GetHashCode() => m_handle.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 @@ -68,6 +69,12 @@ public override string ToString()

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

public override bool Equals(object? obj) =>
obj == (object)this ||
(RuntimeTypeMetadataUpdateHandler.MetadataUpdaterSupportedAndCacheCleared && CacheEquals(obj));

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

#region ICustomAttributeProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

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

namespace System.Reflection
Expand Down Expand Up @@ -46,6 +47,12 @@ internal RuntimeType GetDeclaringTypeInternal()

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

public override bool Equals(object? obj) =>
obj == (object)this ||
(RuntimeTypeMetadataUpdateHandler.MetadataUpdaterSupportedAndCacheCleared && CacheEquals(obj));

public override int GetHashCode() => HashCode.Combine(MetadataToken.GetHashCode(), Module.GetHashCode());
#endregion

#region Object 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.Security;
using System.Text;
Expand Down Expand Up @@ -163,49 +164,56 @@ public override int GetHashCode()
if (IsGenericMethod)
return ValueType.GetHashCodeOfPtr(m_handle);
else
return base.GetHashCode();
return m_handle.GetHashCode();
}

public override bool Equals(object? obj)
{
if (!IsGenericMethod)
return obj == (object)this;
if (IsGenericMethod)
{
// 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.

// 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;

RuntimeMethodInfo? mi = obj as RuntimeMethodInfo;
if (mi == null || !mi.IsGenericMethod)
return false;

if (mi == null || !mi.IsGenericMethod)
return false;
// now we know that both operands are generic methods

// 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;

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();

Type[] lhs = GetGenericArguments();
Type[] rhs = mi.GetGenericArguments();
if (lhs.Length != rhs.Length)
return false;

if (lhs.Length != rhs.Length)
return false;
for (int i = 0; i < lhs.Length; i++)
{
if (lhs[i] != rhs[i])
return false;
}

for (int i = 0; i < lhs.Length; i++)
{
if (lhs[i] != rhs[i])
if (DeclaringType != mi.DeclaringType)
return false;
}

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

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

return obj == (object)this ||
(RuntimeTypeMetadataUpdateHandler.MetadataUpdaterSupportedAndCacheCleared &&
obj is RuntimeMethodInfo m &&
m.MetadataToken == MetadataToken &&
RuntimeTypeHandle.GetModule(m_declaringType).Equals(RuntimeTypeHandle.GetModule(m.m_declaringType)));

return true;
}
#endregion

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 System.Runtime.CompilerServices;
using MdToken = System.Reflection.MetadataToken;

Expand Down Expand Up @@ -496,6 +497,14 @@ public override Type[] GetOptionalCustomModifiers()
m_signature.GetCustomModifiers(PositionImpl + 1, false);
}

public override bool Equals(object? obj) =>
obj == (object)this ||
(RuntimeTypeMetadataUpdateHandler.MetadataUpdaterSupportedAndCacheCleared &&
obj is RuntimeParameterInfo m &&
m_tkParamDef == m.m_tkParamDef &&
GetRuntimeModule()!.Equals(m.GetRuntimeModule()));

public override int GetHashCode() => HashCode.Combine(m_tkParamDef.GetHashCode(), GetRuntimeModule()!.GetHashCode());
#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 @@ -179,6 +180,12 @@ 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) =>
obj == (object)this ||
(RuntimeTypeMetadataUpdateHandler.MetadataUpdaterSupportedAndCacheCleared && CacheEquals(obj));

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

#region PropertyInfo Overrides
Expand Down
Loading