.NET Extension Methods for extending classes - not working in inline code
Nov 09, 2011
Some time ago we had a really weird problem: our extension methods worked fine in code-behind, and when we used then in inline code (asp.net) with a web form they seem to be fine too, intellisence recognized them, no compilation errors arose but when running the code the “ CS0117: 'type' does not contain a definition for 'identifier' ” error appeared.
This error is equivalent to the VB error BC30456 and is related to error CS1061.
To use an extension method with inline code, it’s enough to bring it to scope by adding a <%@ Import %> directive on top of the .aspx page with the namespace where the method is defined. More straightforward, if intellisence recognizes the method after writing the previous directive you’ve done it right.
Well, obviously the solution is not listed in MSDN’s articles about that error, or it is not one of the proposed solutions. We got this error while trying to migrate a CS app to VB. In the web.config there’s a section tagged <compilers> under <system.codedom> which contains a list of compilers used among other things for inline code.
It turned out that in the migrated VB app, we had left a compiler definition for C# only and not added the VB compiler definition. Actually you don’t need the C# definition but it doesn’t hurt either. This is because we migrated the web.config just by changing the defaultLanguage attribute in the <compilation> element.
Most simple apps will work flawlessly just changing the defaultLanguage and the pages code-behind and directives from C# to VB or vice versa. Actually our app worked fine without using extension methods or using them only in code-behind, and was not a trivial app.
For a better understanding, the VB app web.config had:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
The same happens if the C# app web.config has:
<system.codedom>
<compilers>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
Switch those code snippets and voilà!
To dig further, extension methods don’t work because the inline code is compiled with the 2.0 version of the compiler by default (we’re using a VS 2008 project). This can be checked in the detailed compiler output below the error:
But when the right compiler section is in the web.config, the output is:
Extension methods were introduced in Framework version 3.5, although you can manage to get them to work on 2.0 as long as it’s not an ASP.net app, but that’s another story.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.