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 f4e02f566e5b42..550261363f6d1d 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,14 +156,17 @@ internal void ClearCaches() internal sealed class CachingContext { private readonly ConcurrentDictionary _jsonTypeInfoCache = new(); +#if !NETCOREAPP private readonly Func _jsonTypeInfoFactory; +#endif public CachingContext(JsonSerializerOptions options, int hashCode) { Options = options; HashCode = hashCode; - - _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; +#if !NETCOREAPP + _jsonTypeInfoFactory = Options.GetTypeInfoNoCaching; +#endif } public JsonSerializerOptions Options { get; } @@ -172,7 +175,12 @@ 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, _jsonTypeInfoFactory); + public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => +#if NETCOREAPP + _jsonTypeInfoCache.GetOrAdd(type, static (type, options) => options.GetTypeInfoNoCaching(type), Options); +#else + _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory); +#endif public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo);