The problem with the common extension method with inheritance

I am trying to extend the page class to add some new features (ease of use for some methods, as they will be called directly inside the code of this page) in ASP.NET, and I get a strange error:

My method is called SetQuery,
if I type SetQueryin the page class, it is not recognized (yes, I added using [Namespace];),
if I type base.SetQuery, it is visible in IntelliSense, but it does not compile, saying that the extension method or method was not found on the page
if I type (this as Page).SetQueryIt is recognized and works.

Especially the second case seems to me to be a mistake, since IntelliSense recognizes it as an extension method, but not compilation.

Is there any “more natural” way to enter SetQuery when I go, without a cast, etc.?

+5
source share
1 answer

Extension methods always require a (explicit) target, so it is not possible to invoke an extension method through only TheMethodName(). I suspect if you type:

this.SetQuery();

he will work. There is never an implicit this.extension method. Odd but true.

, SetQuery() ; base.SetQuery() , Page, . (this as Page).SetQuery() , this.SetQuery(), , this as Page, , , , no-op - ie this.SetQuery() (this as Page).SetQuery() IL ( SetQuery(), ).

+9

All Articles