How to Check Whether Reflection-Based JSON Serialization Is On in C#
JsonSerializer.IsReflectionEnabledByDefault is true when System.Text.Json may work out your types with reflection, and false when it may only use the type information you give it. Read it at run time to choose how to serialize.
Microsoft's reference: Gets a value that indicates whether unconfigured JsonSerializerOptions instances should default to the reflection-based DefaultJsonTypeInfoResolver. That resolver's own reference: Defines the default, reflection-based JSON contract resolver used by System.Text.Json.
The value comes from the app's settings, not from your code: The value of the property is backed by the "System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault" AppContext setting and defaults to true if unset. The source generation guide: You can programmatically check whether reflection is disabled by using the JsonSerializer.IsReflectionEnabledByDefault property.
Syntax
using System.Text.Json;
bool <reflectionOn> = JsonSerializer.IsReflectionEnabledByDefault;
| Part | What it is |
|---|---|
JsonSerializer.IsReflectionEnabledByDefault | A static, read-only bool, declared as public static bool IsReflectionEnabledByDefault { get; } |
true | Reflection is on: JsonSerializer can work out a type you give it no type information for |
false | Reflection is off: it needs type information, such as a source-generated JsonSerializerContext |
<reflectionOn> | A variable that keeps the value |
The setting behind it is the MSBuild property JsonSerializerIsReflectionEnabledByDefault in the project file, shown after the example.
The Example
A console app called ReflectionSwitch prints the value, builds a JsonSerializerOptions that uses reflection when it's on and a source-generated context when it's off, and writes a to-do note as JSON. It runs twice: as a new console project, then with reflection turned off in the project file.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n ReflectionSwitch
cd ReflectionSwitch
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/ReflectionSwitch/ReflectionSwitch.csproj:
Determining projects to restore...
Restored /Users/you/source/ReflectionSwitch/ReflectionSwitch.csproj (in 47 ms).
Restore succeeded.
dotnet new console -n ReflectionSwitch creates a folder named ReflectionSwitch with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or ReflectionSwitch.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First, read the value:
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
// true: JsonSerializer can work out any type with reflection.
// false: it can only use the type information you give it.
bool reflection = JsonSerializer.IsReflectionEnabledByDefault;
Console.WriteLine($"IsReflectionEnabledByDefault: {reflection}");
IsReflectionEnabledByDefault is a static property, so you read it on the JsonSerializer class, with no object.
Then choose a resolver:
// Use reflection when it's on, and the generated context when it's off.
JsonSerializerOptions options = new()
{
TypeInfoResolver = reflection ? new DefaultJsonTypeInfoResolver() : NoteJsonContext.Default
};
Console.WriteLine($"Resolver: {options.TypeInfoResolver.GetType().Name}");
A resolver hands JsonSerializer the type information for each type. new DefaultJsonTypeInfoResolver() works it out with reflection, and NoteJsonContext.Default is the source-generated context at the end of the file. The source generation guide shows the same choice for this property. GetType().Name prints which resolver was picked.
Last, the note and the context:
// Either way, the note can be written as JSON.
Note note = new("Buy milk", false);
Console.WriteLine(JsonSerializer.Serialize(note, options));
record Note(string Text, bool Done);
[JsonSerializable(typeof(Note))]
partial class NoteJsonContext : JsonSerializerContext;
Note is a record with a text and a done flag. [JsonSerializable(typeof(Note))] and partial make the source generator write the type information for Note into NoteJsonContext.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
IsReflectionEnabledByDefault: True
Resolver: DefaultJsonTypeInfoResolver
{"Text":"Buy milk","Done":false}

In a new console project the property is True, the default the reference describes. So the options got DefaultJsonTypeInfoResolver, and the note came out as {"Text":"Buy milk","Done":false}.
The Complete File

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
// true: JsonSerializer can work out any type with reflection.
// false: it can only use the type information you give it.
bool reflection = JsonSerializer.IsReflectionEnabledByDefault;
Console.WriteLine($"IsReflectionEnabledByDefault: {reflection}");
// Use reflection when it's on, and the generated context when it's off.
JsonSerializerOptions options = new()
{
TypeInfoResolver = reflection ? new DefaultJsonTypeInfoResolver() : NoteJsonContext.Default
};
Console.WriteLine($"Resolver: {options.TypeInfoResolver.GetType().Name}");
// Either way, the note can be written as JSON.
Note note = new("Buy milk", false);
Console.WriteLine(JsonSerializer.Serialize(note, options));
record Note(string Text, bool Done);
[JsonSerializable(typeof(Note))]
partial class NoteJsonContext : JsonSerializerContext;
Turn Reflection Off and Run It Again
The source generation guide: To disable default reflection in your app, set the JsonSerializerIsReflectionEnabledByDefault MSBuild property to false in your project file. Add it to the PropertyGroup in ReflectionSwitch.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
</Project>
Then run the same program again:
dotnet run
IsReflectionEnabledByDefault: False
Resolver: NoteJsonContext
{"Text":"Buy milk","Done":false}
The property read False, so the options got NoteJsonContext, and the note came out the same. The guide adds: The behavior of this property is consistent regardless of runtime, either CoreCLR or Native AOT.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Setting the property in code: JsonSerializer.IsReflectionEnabledByDefault = false; | error CS0200: Property or indexer 'JsonSerializer.IsReflectionEnabledByDefault' cannot be assigned to -- it is read only | Set JsonSerializerIsReflectionEnabledByDefault to false in the project file |
Calling JsonSerializer.Serialize(note) with no type information while the property is False | InvalidOperationException: Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property. | Check the property first, as the example does, or pass a context's type information |
Turning it off with AppContext.SetSwitch after something has read it | The property printed True before the call and still True after it | Use the project file setting: the run above read False from its first line. A SetSwitch before the first read did give False, but the project file is the way the guide documents |
FAQ
Why is it False when I never turned it off?
Trimming and native AOT turn it off too. With PublishTrimmed set to true, dotnet run printed IsReflectionEnabledByDefault: False, and so did a program with PublishAot set to true. The guide: If you don't specify this property and PublishTrimmed is enabled, reflection-based serialization is automatically disabled.
Why does a one-file app run with dotnet run app.cs print False?
A file-based app with no settings printed IsReflectionEnabledByDefault: False. The file-based apps guide: File-based apps enable native ahead-of-time (AOT) compilation by default. Its way out is a line at the top of the file, #:property PublishAot=false: with it, the same check printed True.
Conclusion
JsonSerializer.IsReflectionEnabledByDefault tells your code whether System.Text.Json may use reflection. It's True in a new console project and False when the project file sets JsonSerializerIsReflectionEnabledByDefault to false, when trimming or native AOT is on, and in a file-based app. The property itself is read-only: set the project file property, read the value, and give JsonSerializer a source-generated context when it's False.
Additional Resources
- JsonSerializer.IsReflectionEnabledByDefault — the property and the AppContext setting behind it.
- Disable reflection defaults — the MSBuild property, trimming, and choosing a resolver.
- DefaultJsonTypeInfoResolver — the reflection-based resolver.
- File-based apps — native AOT by default, and how to turn it off.
