Skip to content

Commit d7d4f78

Browse files
Refresh TypeDescriptor cache when attributes change
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 589846a commit d7d4f78

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/MiniValidation/TypeDetailsCache.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,35 @@ internal class TypeDetailsCache
1414
private static readonly PropertyDetails[] _emptyPropertyDetails = Array.Empty<PropertyDetails>();
1515
private readonly ConcurrentDictionary<Type, (PropertyDetails[] Properties, bool RequiresAsync)> _cache = new();
1616

17+
public TypeDetailsCache()
18+
{
19+
TypeDescriptor.Refreshed += args =>
20+
{
21+
if (args.TypeChanged is { } type)
22+
{
23+
_cache.TryRemove(type, out _);
24+
}
25+
else
26+
{
27+
_cache.Clear();
28+
}
29+
};
30+
}
31+
1732
public (PropertyDetails[] Properties, bool RequiresAsync) Get(Type? type)
1833
{
1934
if (type is null)
2035
{
2136
return (_emptyPropertyDetails, false);
2237
}
2338

24-
if (!_cache.ContainsKey(type))
39+
(PropertyDetails[] Properties, bool RequiresAsync) details;
40+
while (!_cache.TryGetValue(type, out details))
2541
{
2642
Visit(type);
2743
}
2844

29-
return _cache[type];
45+
return details;
3046
}
3147

3248
private void Visit(Type type)

0 commit comments

Comments
 (0)