Skip to content

ILLink strips interfaces of an uninstantiated generic base class while the derived class keeps them, causing TypeLoadException #135913

Closed

Description

@Illustar0

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.

Activity

  1. added
    untriaged
    on Oct 16, 2026
  2. added
    area-Tools-ILLink
    on Oct 16, 2026
  3. dotnet-policy-service commented on Oct 16, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/illink
    See info in area-owners.md if you want to be subscribed.

  4. added this to the 10.0.x milestone on Oct 16, 2026
  5. removed
    untriaged
    on Oct 16, 2026
  6. added a commit that references this issue on Oct 29, 2026
    75df8eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-Tools-ILLink

Type

No type

Projects

  • Status
    No status

Relationships

None yet

Development

No branches or pull requests

Issue actions