Question

I came across an issue that makes me think there is bug in the 3.0 framework. When I try to use extension methods I get the following error:

Missing compiler required member
'System.Runtime.CompilerServices.ExtensionAttribute..ctor'

When using this simple code:

public static class StringUtils {
    static void TestExtension(this String targetString) {

    }
}

The only way to make this compile error go away is to add the following code:

namespace System.Runtime.CompilerServices {
    public class ExtensionAttribute : Attribute { }
}

It's been a few months since I have used extensions methods, but I'm pretty sure I didn't have to do this. Has anyone else come across this issue?

Was it helpful?

Solution

I just ran into this problem myself. In my case, it was because I converted a VS 2005/.Net 2.0 project to a VS 2008/.Net 3.5 project. The conversion tool kept references to System.Core 2.0, and I couldn't find an easy way to change the references to System.Core 3.5.

I ended up re-creating the project in VS 2008 from scratch, and it was created with proper references to System.Core 3.5

OTHER TIPS

I have the exact same problem. The error System.Runtime.CompilerServices.ExtensionAttribute..ctor is rather cryptic, and could mean a number of different things.

However, for me It boiled down to the fact that I'm using Newtonsoft.Json.Net. I removed the reference to the file Newtonsoft.Json.Net20.dll, and the re-added it. After this my solution builds again.

The strangest thing is that when I tried to find out what was different after this procedure by using Subversion Diff, nothing appears to have changed.

So I really don't know what removing and re-adding this reference really does, but it does fix my build issue with this particular error message mentioned by the asker.

UPDATE 1:

For those that come across this again, as the comenters pointed out, the proper way to fix this is to Download Json.Net's ZIP, and there should be a 3.5 version, re-reference 3.5 every where you are using Json.Net and delete the old reference, as it is likely referencing an assembly that was built for older versions of .net.

UPDATE 2:

Charlie Flowers points out that the DLL NewtonSoft labels as being for 3.5 is actually not going to work with 3.5. You have to use the DLL they label as being for .net 2.0

in VS, click Project (next to File,Edit,View), select Properties

then in Application tab (you'll notice you're already in 3.5), select the Target Framework to 2.0, then compile (it will error). then put it back again to 3.5, then compile again, the error will disappear

i think it is just a small glitch in Visual Studio, just fool the IDE :-)

I had the same issue in a class library project that I had upgraded from VS 2008 to VS 2010 Beta 2. I hadn't added any extension methods to the project until after the upgrade, then I started seeing the same error.

Adding a class with the following code to the project solved the problem:

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute { }
}

Found the tip on this blog: http://blog.flexforcefive.com/?p=105

Your framework isn't high enough for Extension Methods.
That's a hack for making extension methods work without being in 3.5

What version of .NET are you targetting? The ExtensionAttribute class is shipped in System.Core.dll (.NET 3.5), but you can re-declare it yourself if you want to use extension methods in .NET 2.0/3.0 (with C# 3.0, obviously). In fact, LINQBridge does this.

[update] However, I'm slightly confused, because the error you should see is:

Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff]

A missing System.Core reference will give these symptoms.

This problem is indeed caused by an incorrect reference to version 2 of System.Core . This is normally caused when upgrading from an earlier version of .NET to .NET 3.5 . If it is a website that you are experiencing this problem in then it can be remedied by following the steps below:

1) In web.config add a reference to System.Core v3.5:

<assemblies>
   <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>

2) In web.config add the following as a child of configuration:

<configuration>
   <!--Some other config-->
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
         <assemblyIdentity name="System.Core" publicKeyToken="B77A5C561934E089"/>
         <bindingRedirect oldVersion="2.0.0.0-2.1.0.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
   </assemblyBinding>
</configuration>

For ongoing reference:

I have battled this exact same error message for the last two hours in the .Net 4.0 framework and finally tracked it down to a corrupted reference to the Microsoft.CSharp dll. It was such a frustratingly simple issue. I deleted the offending reference and added a "new" reference to that dll which was located at:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll

Hopefully this will save someone else the time and aggravation of tracking down a solution to this error message.

Extensions are introduced in C# 3.0, which on the other hand was introduced in .NET 3.5 so you can't really use them in .NET 3.0 directly.

Is this a web site project, by chance? Try changing the target framework from .NET 3.5 to an earlier version, and then back to .NET 3.5.

Try: Project, Add Reference, find System Core 3.5.0.0 in the list, and OK to add it.

Just target your VS project to .NET framework 3.5 and above. Most probably you converted your project from a previous version.

I had a version of Elmah.Sandbox that was targetting .net 2.0 in an existing .Net 4.0 project. I added an extension method to my project and the build failed with the "Missing compiler required member" error message.

The Elmah.Sandbox dll was included as part of an early version of ElmahR. Once this was removed, it all built again.

In my case the reason was the wrong NET20 version of Microsoft's AntiXSSLibrary.dll. Replaced with NET35 - the error is gone.

Edit your csproj and make sure those references are included (it wont work by simple 'add reference'...):

<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top