You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json 11 rc.3 breaks source generation into net472 projects #136457
It seems that the STJ source generator fails to accommodate for net472 targeted projects, using UnsafeAccessorAttribute even though it only exists on .NET runtimes.
G:\src\Nerdbank.MessagePack\obj\test\Benchmarks\Debug\net472\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\STJSourceGenerationContext.PocoAsArrayInit.g.cs(114,50): error CS0122: 'UnsafeAccessorAttribute' is inaccessible due to its protection level
Regression?
Yes. This worked fine with System.Text.Json 10.0.10.
Further investigation narrows this down to a false-positive runtime-capability check caused by an internal polyfill in a referenced assembly, rather than a missing .NET Framework fallback.
Both were confirmed by inspecting the referenced assembly (IsPublic == false).
In the source revision used by System.Text.Json 11.0.0-rc.3.26522.148, CanUseUnsafeAccessors checks whether the attribute symbol is non-null. The BenchmarkDotNet shim satisfies that check, so the generator emits unsafe-accessor methods for public init setters, resulting in the reported CS0122 errors.
When the shim is absent, the generator correctly falls back to cached setter delegates created using reflection. That fallback works on .NET Framework.
Isolated reproduction
This does not require Nerdbank.MessagePack or PolyType. Use this project:
usingSystem;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;internalstaticclassProgram{privatestaticvoidMain(){Samplevalue=JsonSerializer.Deserialize("{\"Number\":42,\"Text\":\"hello\"}",Context.Default.Sample)!;if(value.Number!=42||value.Text!="hello"){thrownewInvalidOperationException("Init-only properties were not deserialized correctly.");}Console.WriteLine("net472 source-generated init-only deserialization succeeded.");}}publicsealedclassSample{publicintNumber{get;init;}publicstring?Text{get;init;}}[JsonSerializable(typeof(Sample))]internalpartialclassContext:JsonSerializerContext{}namespaceSystem.Runtime.CompilerServices{internalstaticclassIsExternalInit{}}
Verified locally:
Without the BenchmarkDotNet package reference: builds, runs, and correctly deserializes both init properties.
With that reference (no BenchmarkDotNet API usage required): fails with the same inaccessible UnsafeAccessorAttribute / UnsafeAccessorKind errors.
Repository-specific workaround
In Nerdbank.MessagePack, all consumers of the generated JSON context are already guarded by #if NET, so the context is unused on net472. Excluding it from that target works around the issue without changing benchmark coverage:
Tested the equivalent exclusion through a temporary build target: both net472 and net10.0 built successfully with zero warnings or errors. This is only a workaround for this repository's unused context, not a general fix for applications that need source-generated JSON on .NET Framework.
The issue with platform detection in Roslyn is that it has to rely on heuristics like this. While this particular heuristic could be improved (e.g. by checking the declaring assembly), I would question BDN's decision to polyfill these attributes since they are a runtime feature and not a language feature. Given that a fix here is going to miss the GA release, perhaps asking BDN to walk back these polyfills is the fastest solution.
I thought most runtime features were detected via presence of properties on the RuntimeFeature class. That wouldn't be something a polyfill attribute would mess up.
Activity
AArnott commentedon Oct 26, 2026
@eiriktsarpalis heads up.
dotnet-policy-service commentedon Oct 26, 2026
Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.
AArnott commentedon Oct 26, 2026
Further investigation narrows this down to a false-positive runtime-capability check caused by an internal polyfill in a referenced assembly, rather than a missing .NET Framework fallback.
Root cause
BenchmarkDotNet0.15.8'snetstandard2.0assembly contains internal types named:System.Runtime.CompilerServices.UnsafeAccessorAttributeSystem.Runtime.CompilerServices.UnsafeAccessorKindBoth were confirmed by inspecting the referenced assembly (
IsPublic == false).In the source revision used by
System.Text.Json11.0.0-rc.3.26522.148,CanUseUnsafeAccessorschecks whether the attribute symbol is non-null. The BenchmarkDotNet shim satisfies that check, so the generator emits unsafe-accessor methods for publicinitsetters, resulting in the reported CS0122 errors.When the shim is absent, the generator correctly falls back to cached setter delegates created using reflection. That fallback works on .NET Framework.
Isolated reproduction
This does not require Nerdbank.MessagePack or PolyType. Use this project:
Verified locally:
initproperties.UnsafeAccessorAttribute/UnsafeAccessorKinderrors.Repository-specific workaround
In Nerdbank.MessagePack, all consumers of the generated JSON context are already guarded by
#if NET, so the context is unused onnet472. Excluding it from that target works around the issue without changing benchmark coverage:Tested the equivalent exclusion through a temporary build target: both
net472andnet10.0built successfully with zero warnings or errors. This is only a workaround for this repository's unused context, not a general fix for applications that need source-generated JSON on .NET Framework.eiriktsarpalis commentedon Oct 26, 2026
The issue with platform detection in Roslyn is that it has to rely on heuristics like this. While this particular heuristic could be improved (e.g. by checking the declaring assembly), I would question BDN's decision to polyfill these attributes since they are a runtime feature and not a language feature. Given that a fix here is going to miss the GA release, perhaps asking BDN to walk back these polyfills is the fastest solution.
AArnott commentedon Oct 26, 2026
I thought most runtime features were detected via presence of properties on the
RuntimeFeatureclass. That wouldn't be something a polyfill attribute would mess up.jkotas commentedon Oct 26, 2026
RuntimeFeatures is only used for runtime features that are not exposed via an API.
Fix STJ unsafe accessor detection with polyfilled attributes (dotnet#…