roslyn: base keyword not detect extension method

Basically

image

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)

Most upvoted comments

Why would it be necessary to use extension methods with base? It’s not like you could treat the target any differently than this. 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 using base 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 work

just make a quick example that sometimes I want to use base as the extension method so the type T will be the type of base

For as rare as such a use case would be you could always just call the extension method and supply the generic type argument:

this.DoSomething<B>();

and virtual method will call the base one

Even if the above example would compile and the T generic type parameter resolved correctly to B the call to obj.Do() would still call A.Do, not B.Do, because obj is a A and it’s vtable entry for Do points to A.Do. The only way to call B.Do is from within A using base.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 because base as a keyword isn’t an expression and doesn’t resolve in a value. It’s not possible to get a reference to base which acts like a B.

@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 called obj.GetType() it would still return typeof(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 for base to resolve extension methods wouldn’t change that.