roslyn: base keyword not detect extension method
Basically
this
keyword work fine without error. So base
should not be differenced
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 22 (7 by maintainers)
Basically
this
keyword work fine without error. So base
should not be differenced
Why would it be necessary to use extension methods with
base
? It’s not like you could treat the target any differently thanthis
. If anything that would probably lead to confusion as you might expect that the extension method could call virtual members of the base type directly rather than invoking the overridden member on the derived type. You probably shouldn’t be usingbase
in any scenario other than where you intend to bypass virtual method dispatch or invoke a parent constructor.Your reminder also remind me when I want to call function in base class of the base class.
base.base.DoSomething()
could not workFor as rare as such a use case would be you could always just call the extension method and supply the generic type argument:
Even if the above example would compile and the
T
generic type parameter resolved correctly toB
the call toobj.Do()
would still callA.Do
, notB.Do
, becauseobj
is aA
and it’s vtable entry forDo
points toA.Do
. The only way to callB.Do
is from withinA
usingbase.Do()
directly.Note that you cannot use
base
for anything except calling a parent constructor or calling a parent member. You cannot take a reference to it. You cannot pass it to any methods. This is becausebase
as a keyword isn’t an expression and doesn’t resolve in a value. It’s not possible to get a reference tobase
which acts like aB
.@Thaina It can’t ever be type safe. Your base class’s base class has effectively said, whether on purpose or by accident, you may not call this method this way. If that’s wrong, the solution is to refactor the base class’s base class. Otherwise you’re breaking the type safety rule which the base class’s base class is enforcing (whether on purpose or by accident).
What you’re requesting is pretty much the same as saying, “I know this library made this class
internal
, but I want a type-safe way to access it from my assembly.” That’s a contradiction in terms. Type safety means not accessing a library internal class from your assembly.Likewise, type safety also means not invoking a base.base method implementation. If it was otherwise, there would be no way for me (as the base class implementor) to prevent you from digging out of my level of abstraction to the base class beneath and subverting my logic, as a public API no less.
@Thaina
What you described wouldn’t be possible anyway. The
base
keyword doesn’t change the type of the instance at all. If the extension method calledobj.GetType()
it would still returntypeof(Test)
. If you attempted to invoke a virtual method it would still end up calling the overridden method. There’s nothing in the C# language (or the CLR for that matter) which supports that behavior and allowing forbase
to resolve extension methods wouldn’t change that.