Skip to content

[Mono] Invalid covariant interface calls #48512

@shadow-cs

Description

@shadow-cs

Describe the bug

If implementing an interface with covariant method result invalid implementation is called. Please see the code sample (better than thousand words, right).

This may be related to mono/mono#7009.

I'm aware that this may be a Mono problem but since I'm unaware of the state of .NET 6 migration I'm posting it here so others may find it easier, please redirect me to the correct repo if needed (Mono or .NET runtime???).

To Reproduce

public interface IFactory<out T>
{
  T Get();
}

public class Foo
{
}

public class Bar : Foo
{
}

public class FooFactory : IFactory<Foo>
{
  public Foo Get() => new Foo();
}

public class BarFactory : FooFactory, IFactory<Bar>
{
  public new Bar Get() => new Bar();
}

Test method:

FooFactory c = new BarFactory();
IFactory<Foo> ic = c;
Console.WriteLine($"Result: {ic.Get()}");

If I executed as a console app on either .NET 5 or 6 I get Result: Namespace.Bar.

If executed in Blazor I get Result: BlazorDemo.Pages.Counter+Foo (I put this in a razor file for simplicity). Also both .NET 5 and 6.

Further technical details

  • ASP.NET Core 5.0.3 and 6.0.0-preview.1.21103.6

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions