Extension methods don’t compile? No panic!
I came across to a really strange problem today. Some extension methods that I have copied from another project just did not want to be compiled at all.
‘string’ does not contain a definition for ‘MethodName’.
I have checked everything, including clearing the temporary ASP.NET files within framework folder (C:\Windows\Microsoft.NET\Framework\…), restarting Visual Studio and even restarting the OS. The target framework of the projects were all set to 3.5 as well. But unfortunately, nothing helped. Finally, when I compared what kind of configuration I could be missing from web.config, I have found out the missing (and the killer) part.
If you have a similar problem, try adding the following configuration section in your project’s web.config file.
<system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <providerOption name="CompilerVersion" value="v3.5"/> <providerOption name="WarnAsError" value="false"/> </compiler> </compilers> </system.codedom>