简体   繁体   中英

Extension methods with generics

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..

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM