runtime: C# 9 covariant return types: Still can get System.TypeLoadException when using generics

Description

This looks like https://github.com/dotnet/core/issues/5319 but with generics.

The C # 9.0 overridden virtual function works fine when the return type is a covariant derived type, continues to work if the return type is a covariant derived type that contains the overridden function. But System.TypeLoadException is thrown at runtime if you add some generics. As before, everything builds without errors and warnings.

Code to reproduce

using System;

var c = new AChild();

Console.WriteLine("Hello World!");

public abstract class A<T>
{
    public abstract B<T> GetB();
}

public class AChild : A<string>
{
    public override BChild GetB() => null;
}

public class B<T>
{
}

public class BChild : B<string>
{
}

Error:

$ dotnet run
Unhandled exception. System.TypeLoadException: Return type in method 'AChild.GetB()' on type 'AChild' from assembly 'N5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with base type method 'A`1[System.__Canon].GetB()'.
$ dotnet --version
5.0.100

Versions

5.0.100-rc.2.20479.15 5.0.100

_Originally posted by @ASolomatin in https://github.com/dotnet/core/issues/5319#issuecomment-722769903_

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 16 (8 by maintainers)

Most upvoted comments

@ASolomatin I’ll verify it with the latest version to make sure this is not yet another problem.

@ASolomatin what version of runtime have you reproed it with? The latest fix is not in any released .NET 5 versions, it will be part of 5.0.2.