Repository navigation
Description
Description
The .NET 11 trimmer (Microsoft.NET.ILLink.Tasks) removes the public parameterless constructor of a type that is used only as the generic argument of a new()-constrained generic custom attribute, when that attribute is reached only via reflection (Type.GetCustomAttributes).
The type's shell is kept (its name is referenced from attribute metadata), but its method table is emptied. When the runtime later materializes the generic attribute and validates the new() constraint, the constraint is unsatisfiable and a TypeLoadException is thrown from inside GetCustomAttributes.
This is a regression in .NET 11: the exact same source and trim settings work correctly on .NET 10.
No IL trim warning is emitted — this is a silent hole.
Minimal reproduction
Program.cs:
using System;
using Repro;
typeof(CheckBox).GetCustomAttributes(false);
Console.WriteLine("RESULT: OK");
namespace Repro
{
public class Handler { }
class MyAttribute<T> : Attribute where T : new() { }
[My<Handler>] class CheckBox { }
}App.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>osx-arm64</RuntimeIdentifier>
</PropertyGroup>
</Project>Publish and run:
dotnet publish -c Release -r osx-arm64 -o ./bin/pub
./bin/pub/AppExpected behavior
RESULT: OK (as it does untrimmed, and as it does when trimmed on .NET 10).
Actual behavior
Unhandled exception. System.TypeLoadException: GenericArguments[0], 'Repro.Handler', on 'Repro.MyAttribute`1[T]' violates the constraint of type parameter 'T'.
at System.ModuleHandle.ResolveType(...)
at System.ModuleHandle.ResolveTypeHandle(...)
at System.Reflection.RuntimeModule.ResolveType(...)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(...)
at System.Reflection.CustomAttribute.AddCustomAttributes(...)
at System.Reflection.CustomAttribute.GetCustomAttributes(...)
at System.RuntimeType.GetCustomAttributes(Boolean inherit)
at Program.<Main>$(String[] args) in Program.cs:line 4
The constraint violation is bogus: Repro.Handler does have a public parameterless constructor in source. The trimmer removed it, leaving the new() constraint unsatisfiable at runtime.
Confirmed mechanism
Inspecting the trimmed App.dll shows Repro.Handler's parameterless constructor has been removed (the type is kept as an empty shell). Two independent mitigations confirm this is the cause:
- Removing the
where T : new()constraint fromMyAttribute<T>→ trimmed app passes. - Adding
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(Handler))]from reachable code → ctor preserved, trimmed app passes.
Notably, putting [DynamicallyAccessedMembers(PublicParameterlessConstructor)] on the attribute's T type parameter does not preserve the constructor.
Regression range
Same source, same TrimMode=full:
| Target | Microsoft.NET.ILLink.Tasks |
Result |
|---|---|---|
| net11.0 (untrimmed) | — | ✅ RESULT: OK |
| net10.0 trimmed | 10.0.5 |
✅ RESULT: OK |
| net11.0 trimmed | 11.0.0-preview.4.26230.115 |
❌ TypeLoadException |
Configuration
- SDK:
11.0.100-preview.4.26230.115 - RID:
osx-arm64(macOS, Apple Silicon) PublishTrimmed=true,TrimMode=full,SelfContained=true
Other information
This pattern (a new()-constrained generic attribute resolved via reflection) is used by .NET MAUI's ElementHandlerAttribute<THandler>, where it caused CheckBox handler resolution to crash at startup in net11 Release builds — see dotnet/maui#35665. MAUI has a workaround, but the underlying trimmer behavior looks like a bug.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon May 29, 2026
Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.
simonrozsival commentedon May 29, 2026
Bisected: regression introduced mid-P4 (works on P1/P2/P3 and early P4)
Pinning
Microsoft.NET.ILLink.Tasksto specific builds (same SDK, same minimal repro, CoreCLRTrimMode=full):11.0.0-preview.1.26104.11811.0.0-preview.2.26159.11211.0.0-preview.3.26118.10911.0.0-preview.3.26219.104(last P3)11.0.0-preview.4.26181.116(first P4)11.0.0-preview.4.26207.10811.0.0-preview.4.26215.10111.0.0-preview.4.26215.12111.0.0-preview.4.26222.14211.0.0-preview.4.26224.12211.0.0-preview.4.26230.115(Date stamps decoded via Arcade's
(year-2000)*1000 + month*50 + day.)So the regression was introduced by a
src/tools/illinkchange merged between 2026-04-15 and 2026-04-22. VMR boundary commits (dotnet/dotnet): goodab01524bbb2ef1eea0ffaef161b3ef5686e8f256→ badaae32bdb43e214583a15e3393e38c889b9447ec4.Candidate illink PRs merged in that window (for the linker team to pinpoint):
It works on .NET 11 Preview 3 and the first ~5 weeks of Preview 4; it broke only in the 2026-04-22 ILLink build.
jtschuster commentedon May 29, 2026
Thank you for the bisect info, that's super helpful!
A couple of the candidate PRs look benign:
ILTrim is an experimental trimmer that isn't the trimmer we ship.
This only touches the analyzer and shouldn't affect trimming.
Since the repro doesn't use the TypeMap I imagine this is unrelated.
The rest could potentially cause issues
The reordering of steps could be messing with something, but it wouldn't be my first guess.
This one moves around type marking order. This would be my first guess to check.
sbomer commentedon Jul 21, 2026
I think it was regressed in https://github.com/dotnet/runtime/pull/119419/changes#diff-173496ee0ea5037ffe1a0f2cae5ad637aee0016745c24cfb24ca081b8dd58608L2944 (just for future reference).