private const string CreateValueInfoMethodName = "CreateValueInfo";
private const string CtorParamInitMethodNameSuffix = "CtorParamInit";
private const string DefaultOptionsStaticVarName = "s_defaultOptions";
- private const string DefaultContextBackingStaticVarName = "s_defaultContext";
private const string OriginatingResolverPropertyName = "OriginatingResolver";
private const string InfoVarName = "info";
private const string NumberHandlingPropName = "NumberHandling";
writer.WriteLine();
writer.WriteLine($$"""
- private static {{contextTypeRef}}? {{DefaultContextBackingStaticVarName}};
-
/// <summary>
/// The default <see cref="{{JsonSerializerContextTypeRef}}"/> associated with a default <see cref="{{JsonSerializerOptionsTypeRef}}"/> instance.
/// </summary>
- public static {{contextTypeRef}} Default => {{DefaultContextBackingStaticVarName}} ??= new {{contextTypeRef}}(new {{JsonSerializerOptionsTypeRef}}({{DefaultOptionsStaticVarName}}));
+ public static {{contextTypeRef}} Default { get; } = new {{contextTypeRef}}(new {{JsonSerializerOptionsTypeRef}}({{DefaultOptionsStaticVarName}}));
/// <summary>
/// The source-generated options associated with this context.
}).Dispose();
}
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
+ [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ public static void JsonSerializerContext_GeneratedDefault_IsSingleton()
+ {
+ RemoteExecutor.Invoke(
+ static () =>
+ {
+ const int Count = 30;
+ var contexts = new MetadataContext[Count];
+ Parallel.For(0, Count, i => contexts[i] = MetadataContext.Default);
+
+ Assert.All(contexts, ctx => Assert.Same(MetadataContext.Default, ctx));
+
+ }).Dispose();
+ }
+
[Fact]
public static void SupportsReservedLanguageKeywordsAsProperties()
{