Extension methods with generics

北慕城南 提交于 2020-01-11 06:43:19

问题


I was looking at this question and i was curious why this deosn't compile.

Given this code, can anyone explain why the call to IBase.Test() doesn't resolve to the correct extension method?

public interface IBase { }

public interface IChildA : IBase { }

public interface IChildB : IBase { }

public static class BaseExtensions
{
    public static IBase Test(this IBase self) { return self; }
    public static T Test<T>(this T self) where T : IChildB { return self; }
}

public static class TestClass
{
    public static void Test()
    {
        IChildA a = null; //Yeh i know its null but just testing for compile here..
        IBase firstTry = a.Test();  //Cannot resolve to BaseExtensions.Test(this IBase obj)
        IBase secondTry = ((IBase)a).Test();  //Resolves to BaseExtensions.Test(this IBase obj)

        IChildB b = null;
        IChildB touchedB = b.Test();
    }
}

The error i get is

Error 166 The type 'IChildA' cannot be used as type parameter 'T' in the generic type or method 'BaseExtensions.Test<T>(T)'. There is no implicit reference conversion from 'IChildA' to 'IChildB'.

I had a feeling it is because it would be ambigous for anything that implements IChildB and wouldn't know which extension method to use, but the error message doesn't moan about that side of it and if you remove the IBase firstTry = a.Test(); line then it compiles fine..


回答1:


Okay, the problem is that in the overload resolution process, the compiler finds all applicable candidate methods without checking the generic constraints specified in the method, picks the most specific one, and then checks the generic constraints.

In this case, the generic method is more specific than the non-generic one (as after type parameter substitution, it's effectively a method with an IChildA parameter instead of an IBase parameter) - but it then fails the constraints.

I have a blog post explaining this in more detail, and another one using it in a horrible way.



来源:https://stackoverflow.com/questions/5403370/extension-methods-with-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!