dotnet/roslyn

Generic interface inheritance conflict with extension method

Open

#16,843 建立於 2017年1月31日

在 GitHub 查看
 (11 留言) (0 反應) (0 負責人)C# (4,257 fork)batch import
Area-CompilersBugConcept-Diagnostic Clarityhelp wanted

倉庫指標

Star
 (20,414 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 256 個 PR)

描述

I write 2 class inherit each other and each implement IEnumerable with difference generic type

Like this

public class A : IEnumerable<object>
{
    public IEnumerator<object> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

public class B : A, IEnumerable<string>
{
    public new IEnumerator<string> GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

////

	static void Test()
	{
		var a = new A();
		var b = new B();

		a.Select((item,i) => i);
		b.Select((item,i) => i);
	}

Instance from A work fine but B cannot use all generic function in System.Linq

image

It seem B was seen by compiler that it is just IEnumerable but not IEnumerable<string>. It still have Cast<> and OfType<> exposed

image

貢獻者指南