title: Escape Catch-22 with extension attributes in .NET 2.0
body: <p>How can a single .NET assembly, targeting 2.0, 3.0, 3.5, 4.0, and 4.5 concurrently, support extension methods for both C# and VB.NET consumers?</p>
  
  <p>The standard suggestion is to add this:</p>
  
  <pre><code>namespace System.Runtime.CompilerServices
  {
    public sealed class ExtensionAttribute : Attribute { }
  }
  </code></pre>
  
  <p>This the approach suggested by <a href="http://www.danielmoth.com/Blog/Using-Extension-Methods-In-Fx-20-Projects.aspx" rel="nofollow noreferrer">more</a> than <a href="http://devhawk.net/2008/09/17/dlr-namespace-change-fire-drill/" rel="nofollow noreferrer">one Microsoft employee</a> and was even featured in <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx#S7" rel="nofollow noreferrer">MSDN magazine</a>. It's <a href="http://www.codethinked.com/using-extension-methods-in-net-20" rel="nofollow noreferrer">widely</a> hailed by <a href="http://kohari.org/2008/04/04/extension-methods-in-net-20/" rel="nofollow noreferrer">many bloggers</a> as having 'no ill effects'. </p>
  
  <p>Oh, except it will cause a compiler error from a VB.NET project targeting .NET 3.5 or higher.</p>
  
  <p>The authors of Microsoft.Core.Scripting.dll <a href="http://devhawk.net/2008/09/17/dlr-namespace-change-fire-drill/" rel="nofollow noreferrer">figured it out</a>, and changed 'public' to 'internal'.</p>
  
  <pre><code>namespace System.Runtime.CompilerServices
  {
    internal sealed class ExtensionAttribute : Attribute { }
  }
  </code></pre>
  
  <p>Which seemed to solve the VB compatibility issue.</p>
  
  <p>So I trustingly used that approach for the latest version (3.2.1) of the <a href="http://imageresizing.net" rel="nofollow noreferrer">widely-used ImageResizing.Net library</a>.</p>
  
  <p>But <strong>then</strong>, we start getting this compiler error (<a href="https://stackoverflow.com/questions/10990536/compilation-errors-with-imageresizer-3-2-1">original report</a>), more or less randomly, for certain users targeting .NET 3.5+.</p>
  
  <pre><code>Error 5 Missing compiler required member
  'System.Runtime.CompilerServices.ExtensionAttribute..ctor'
  </code></pre>
  
  <p>Because the MSBuild/VisualStudio compiler apparently doesn't bother to look at scoping rules when resolving naming conflicts, and the order of assembly references plays a not-quite-docuemented role, I don't fully understand why and when this happens.</p>
  
  <p>There are a <a href="https://stackoverflow.com/a/10996336/166893">few hacky workarounds</a>, like changing the assembly namespace, recreating the project file, deleting/readding System.Core, and fiddling with the target version of the .NET framework. Unfortunately, none of those workarounds are 100% (except aliasing, but that's an unacceptable pain).</p>
  
  <p>How can I fix this while</p>
  
  <ol>
  <li>Maintaining support for extension method use within the assembly, </li>
  <li>Maintaining support for .NET 2.0/3.0</li>
  <li>Not requiring multiple assemblies for each .NET framework version. </li>
  </ol>
  
  <p>Or, is there a hotfix to make the compiler pay attention to scoping rules?</p>
  
  <p>Related questions on SO that don't answer this question</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/2277365/166893">C# Extension methods in .NET 2.0</a></li>
  <li><a href="https://stackoverflow.com/questions/783155/using-extension-methods-with-net-framework-2-0?rq=1">Using Extension Methods with .NET Framework 2.0</a></li>
  <li><a href="https://stackoverflow.com/questions/546819/strange-warning-about-extensionattribute">strange warning about ExtensionAttribute</a></li>
  <li><a href="https://stackoverflow.com/questions/835592/ambigious-reference-for-extensionattribute-when-using-iron-python-in-asp-net">Ambigious reference for ExtensionAttribute when using Iron Python in Asp.Net</a></li>
  <li><a href="https://stackoverflow.com/questions/3389832/should-i-support-net-2-0">Should I support .NET 2.0?</a></li>
  <li><a href="https://stackoverflow.com/questions/1522605/using-extension-methods-in-net-2-0?rq=1">Using extension methods in .NET 2.0?</a></li>
  </ul>