Why do I need the this. qualifier to use an extension method in a master page?
-
06-07-2019 - |
Question
I have the following line of code in some master page code behind, but it fails without the this.
Why is that?
Repeater rep = this.FindControlsByIdRegEx("maintTableRepeater")[0] as Repeater;
This is in the master page Load event, and the extension method is defined as;
public static List<Control> FindControlsByIdRegEx(this Control control, string idPattern)
Solution
Extension methods must have an instance to "hang off of".
A method call without an object always refers to the method with that signature - in the same instance. Extension methods are not part of the instance. They are static methods that live elsewhere in the system, and use a little bit of compiler sugar - a trick, really - to look like they hang off of an object for ease of coding.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow