-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add MetadataUpdateHandlerAttribute #50954
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...System.Private.CoreLib/src/System/Reflection/Metadata/RuntimeTypeMetadataUpdateHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Reflection.Metadata; | ||
|
||
[assembly: MetadataUpdateHandler(typeof(RuntimeTypeMetadataUpdateHandler))] | ||
|
||
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 | ||
{ | ||
public static void BeforeUpdate(Type? type) | ||
{ | ||
if (type is RuntimeType rt) | ||
{ | ||
rt.ClearCache(); | ||
} | ||
|
||
// TODO: https://github.com/dotnet/runtime/issues/50938 | ||
// Do we need to clear the cache on other types, e.g. ones derived from this one? | ||
// Do we need to clear a cache on any other kinds of types? | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...s/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdateHandlerAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace System.Reflection.Metadata | ||
{ | ||
/// <summary>Specifies a type that should receive notifications of metadata updates.</summary> | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
public sealed class MetadataUpdateHandlerAttribute : Attribute | ||
{ | ||
/// <summary>Initializes the attribute.</summary> | ||
/// <param name="handlerType">A type that handles metadata updates and that should be notified when any occur.</param> | ||
public MetadataUpdateHandlerAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type handlerType) => | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HandlerType = handlerType; | ||
|
||
/// <summary>Gets the type that handles metadata updates and that should be notified when any occur.</summary> | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] | ||
public Type HandlerType { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/libraries/System.Runtime.Loader/tests/MetadataUpdateHandlerAttributeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
mikem8361 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
namespace System.Reflection.Metadata | ||
{ | ||
public class MetadataUpdateHandlerAttributeTest | ||
{ | ||
[Fact] | ||
public void Ctor_RoundtripType() | ||
{ | ||
Type t = typeof(MetadataUpdateHandlerAttributeTest); | ||
var a = new MetadataUpdateHandlerAttribute(t); | ||
Assert.Same(t, a.HandlerType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/libraries/System.Runtime/tests/System/Reflection/ReflectionCacheTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.Reflection.Tests | ||
{ | ||
public class ReflectionCacheTests | ||
{ | ||
[Fact] | ||
public void GetMethod_MultipleCalls_SameObjects() | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
MethodInfo mi1 = typeof(ReflectionCacheTests).GetMethod(nameof(GetMethod_MultipleCalls_SameObjects)); | ||
Assert.NotNull(mi1); | ||
|
||
MethodInfo mi2 = typeof(ReflectionCacheTests).GetMethod(nameof(GetMethod_MultipleCalls_SameObjects)); | ||
Assert.NotNull(mi2); | ||
|
||
Assert.Same(mi1, mi2); | ||
} | ||
|
||
[ActiveIssue("https://github.com/dotnet/runtime/issues/50978", TestRuntimes.Mono)] | ||
[Fact] | ||
public void GetMethod_MultipleCalls_ClearCache_DifferentObjects() | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Type updateHandler = typeof(Type).Assembly.GetType("System.Reflection.Metadata.RuntimeTypeMetadataUpdateHandler", throwOnError: true, ignoreCase: false); | ||
MethodInfo beforeUpdate = updateHandler.GetMethod("BeforeUpdate", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(Type) }); | ||
Assert.NotNull(beforeUpdate); | ||
|
||
MethodInfo mi1 = typeof(ReflectionCacheTests).GetMethod(nameof(GetMethod_MultipleCalls_ClearCache_DifferentObjects)); | ||
Assert.NotNull(mi1); | ||
Assert.Equal(nameof(GetMethod_MultipleCalls_ClearCache_DifferentObjects), mi1.Name); | ||
|
||
beforeUpdate.Invoke(null, new object[] { typeof(ReflectionCacheTests) }); | ||
|
||
MethodInfo mi2 = typeof(ReflectionCacheTests).GetMethod(nameof(GetMethod_MultipleCalls_ClearCache_DifferentObjects)); | ||
Assert.NotNull(mi2); | ||
Assert.Equal(nameof(GetMethod_MultipleCalls_ClearCache_DifferentObjects), mi2.Name); | ||
|
||
Assert.NotSame(mi1, mi2); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be enough to just set the GCHandle target to null. The GCHandle is weakhandle and so the code should be generally prepared to handle the null case.
#51070