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

Call TypeDescriptor.GetProperties before calling Refresh in ReflectionCachesUpdateHandler #51604

Closed
wants to merge 2 commits into from
Closed
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,8 @@

#nullable enable
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Metadata;

Expand All @@ -12,28 +14,38 @@ namespace System.ComponentModel
{
internal static class ReflectionCachesUpdateHandler
{
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The actual properties retrieved by GetProperties do not matter.")]
public static void BeforeUpdate(Type[]? types)
{
// ReflectTypeDescriptionProvider maintains global caches on top of reflection.
// Clear those.
ReflectTypeDescriptionProvider.ClearReflectionCaches();

// Each type descriptor may also cache reflection-based state that it gathered
// from ReflectTypeDescriptionProvider. Clear those as well.
if (types is not null)
try
{
foreach (Type type in types)
// ReflectTypeDescriptionProvider maintains global caches on top of reflection.
// Clear those.
ReflectTypeDescriptionProvider.ClearReflectionCaches();

// Each type descriptor may also cache reflection-based state that it gathered
// from ReflectTypeDescriptionProvider. Clear those as well.
if (types is not null)
{
TypeDescriptor.Refresh(type);
foreach (Type type in types)
{
TypeDescriptor.Refresh(type);
}
}
}
else
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
else
{
TypeDescriptor.Refresh(assembly);
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
TypeDescriptor.GetProperties(assembly); // must call before calling Refresh
TypeDescriptor.Refresh(assembly);
}
}
}
catch (Exception ex)
{
// Eat any failures to prevent attempts to clear caches from tearing down an app.
Debug.Fail(ex.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace System.ComponentModel.Tests
{
[SimpleUpdateTest]
[Collection("NoParallelTests")]
public class ReflectionCachesUpdateHandlerTests
{
[Fact]
Expand All @@ -26,7 +26,4 @@ public void ReflectionCachesUpdateHandler_CachesCleared()
Assert.NotSame(ac1[0], ac3[0]);
}
}

[AttributeUsage(AttributeTargets.All)]
internal sealed class SimpleUpdateTestAttribute : Attribute { }
}