Skip to content

Commit 007504d

Browse files
Fixed ReflectTypeDescriptionProvider keeping cached type in the dictionary after ReflectionCachesUpdateHandler caches cleanup
1 parent 0a2d601 commit 007504d

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.ReflectedTypeData.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -547,22 +547,6 @@ internal PropertyDescriptorCollection GetProperties()
547547

548548
return t;
549549
}
550-
551-
/// <summary>
552-
/// Refreshes the contents of this type descriptor. This does not
553-
/// actually requery, but it will clear our state so the next
554-
/// query re-populates.
555-
/// </summary>
556-
internal void Refresh()
557-
{
558-
_attributes = null;
559-
_events = null;
560-
_properties = null;
561-
_converter = null;
562-
_editors = null;
563-
_editorTypes = null;
564-
_editorCount = 0;
565-
}
566550
}
567551
}
568552
}

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,16 +1413,11 @@ private static PropertyDescriptor[] ReflectGetPropertiesImpl(Type type)
14131413
}
14141414

14151415
/// <summary>
1416-
/// Refreshes the contents of this type descriptor. This does not
1417-
/// actually requery, but it will clear our state so the next
1418-
/// query re-populates.
1416+
/// Completely removed the reference to this type descriptor.
14191417
/// </summary>
1420-
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern",
1421-
Justification = "ReflectedTypeData is not being created here, just checking if was already created.")]
1422-
internal void Refresh(Type type)
1418+
internal void Remove(Type type)
14231419
{
1424-
ReflectedTypeData? td = GetTypeData(type, createIfNeeded: false);
1425-
td?.Refresh();
1420+
_typeData.TryRemove(type, out _);
14261421
}
14271422

14281423
/// <summary>

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectionCachesUpdateHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public static void ClearCache(Type[]? types)
2323
{
2424
foreach (Type type in types)
2525
{
26-
TypeDescriptor.Refresh(type);
26+
TypeDescriptor.ClearCache(type);
2727
}
2828
}
2929
else
3030
{
3131
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
3232
{
33-
TypeDescriptor.Refresh(assembly);
33+
TypeDescriptor.ClearCache(assembly);
3434
}
3535
}
3636
}

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,7 @@ private static void Refresh(object component, bool refreshReflectionProvider)
23462346
if (provider.IsPopulated(type))
23472347
{
23482348
found = true;
2349-
provider.Refresh(type);
2349+
provider.Remove(type);
23502350
}
23512351
}
23522352
}
@@ -2430,7 +2430,7 @@ public static void Refresh(Type type)
24302430
if (provider.IsPopulated(type))
24312431
{
24322432
found = true;
2433-
provider.Refresh(type);
2433+
provider.Remove(type);
24342434
}
24352435
}
24362436
}
@@ -2493,7 +2493,7 @@ public static void Refresh(Module module)
24932493

24942494
foreach (Type populatedType in populatedTypes)
24952495
{
2496-
provider.Refresh(populatedType);
2496+
provider.Remove(populatedType);
24972497
refreshedTypes ??= new Hashtable();
24982498
refreshedTypes[populatedType] = populatedType;
24992499
}
@@ -2530,6 +2530,32 @@ public static void Refresh(Assembly assembly)
25302530
}
25312531
}
25322532

2533+
internal static void ClearCache(Type type)
2534+
{
2535+
Refresh(type);
2536+
s_defaultProviderInitialized.TryRemove(type, out _);
2537+
}
2538+
2539+
internal static void ClearCache(Assembly assembly)
2540+
{
2541+
foreach (Module module in assembly.GetModules())
2542+
{
2543+
// Invalidate the provider for the types in the module.
2544+
Refresh(module);
2545+
2546+
// And the default providers.
2547+
IEnumerator<KeyValuePair<Type, object?>> e = s_defaultProviderInitialized.GetEnumerator();
2548+
while (e.MoveNext())
2549+
{
2550+
Type type = e.Current.Key;
2551+
if (type.Module.Equals(module))
2552+
{
2553+
s_defaultProviderInitialized.TryRemove(type, out _);
2554+
}
2555+
}
2556+
}
2557+
}
2558+
25332559
[EditorBrowsable(EditorBrowsableState.Advanced)]
25342560
public static Type ComObjectType
25352561
{

0 commit comments

Comments
 (0)