diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs index b7461d1bacb9a0..f4e02f566e5b42 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs @@ -156,11 +156,14 @@ internal void ClearCaches() internal sealed class CachingContext { private readonly ConcurrentDictionary _jsonTypeInfoCache = new(); + private readonly Func _jsonTypeInfoFactory; public CachingContext(JsonSerializerOptions options, int hashCode) { Options = options; HashCode = hashCode; + + _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; } public JsonSerializerOptions Options { get; } @@ -169,7 +172,8 @@ public CachingContext(JsonSerializerOptions options, int hashCode) // If changing please ensure that src/ILLink.Descriptors.LibraryBuild.xml is up-to-date. public int Count => _jsonTypeInfoCache.Count; - public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, Options.GetTypeInfoNoCaching); + public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory); + public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo); public void Clear()