Repository navigation
Description
Description
When a type is only reached through typeof, ILLink removes the interface implementations (and the explicit implementation methods) of its generic abstract base class, but keeps the same interfaces on the derived class. The derived class can then no longer be loaded on CoreCLR. The publish produces no trim warnings.
Reproduction Steps
Repro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>Program.cs:
using System.Collections;
// Only typeof reaches Derived<>; nothing instantiates Base<T> or Derived<T>.
Console.WriteLine(Holder.DerivedType.Name);
foreach (var i in new List<int> { 1 }) Console.WriteLine(i); // keeps IEnumerable<T> itself
public abstract class Base<T> : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator() => Enumerable.Empty<T>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public interface IDerived<T> : IEnumerable<T>;
public class Derived<T> : Base<T>, IDerived<T>;
public static class Holder
{
public static readonly Type DerivedType = typeof(Derived<>);
}dotnet publish -c Release -r win-x64 -o outout\Repro.exe
Expected behavior
Prints Derived`1 and 1, as dotnet run does. The publish reports no trim warnings, so the trimmed app should behave the same.
Actual behavior
Unhandled exception. System.TypeInitializationException: The type initializer for 'Holder' threw an exception.
---> System.TypeLoadException: Method 'GetEnumerator' in type 'Derived`1' from assembly 'Repro, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
at Holder..cctor()
The trimmed IL:
.class public abstract auto ansi beforefieldinit Base`1 extends [System.Private.CoreLib]System.Object
.class public auto ansi beforefieldinit Derived`1 extends Base`1<T>
implements System.Collections.Generic.IEnumerable`1<T>, System.Collections.IEnumerable
Base<T> loses its interface implementations and its explicit IEnumerable.GetEnumerator, while Derived<T> still lists IEnumerable (through the recursive interfaces of IDerived<T>), which only the removed base method implemented.
Regression?
Unknown.
Known Workarounds
Root the base class interfaces:
[DynamicDependency(DynamicallyAccessedMemberTypes.Interfaces, typeof(Base<>))]
Configuration
- .NET SDK 10.0.401, Microsoft.NET.ILLink.Tasks 10.0.12
- Windows 11, win-x64, net10.0
- Native AOT publish of the same code works; only trimmed CoreCLR apps fail.
Other information
Possibly related: #98536 (trimmer relies on recursive interfaceImpls), #103317 (closed without merging; made the trimmer consider interfaces that a type implements through its base types), #109850.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Oct 16, 2026
Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.
keep `CollectionBase.GetEnumerator` in trimmed builds
ILLink: keep base-type interfaces when a derived type is relevant to …
Runic Desktop: start and answer calls when published trimmed
Native server: Kestrel keeps the trimming DynamicDependency; inventor…