59

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?

royhowie
  • 11,075
  • 14
  • 50
  • 67
Korbin
  • 1,788
  • 2
  • 18
  • 29
  • I have the target framework set to 3.5 in the project properties. – Korbin Oct 15 '08 at 17:37
  • In which case, I wonder if one of the assemblies you reference *also* declare this attribute (courtesy of being upgraded from .NET 2.0 with C# 3.0), and the compiler is having a hard time picking which one to use? Do you get this problem in a vanilla (clean) project with just the StringUtils etc? – Marc Gravell Oct 15 '08 at 17:44
  • (meaning: not System.Core.dll, and perhaps with an internal constructor) – Marc Gravell Oct 15 '08 at 17:45
  • Related: [Catch-22 with self-defined ExtensionAttribute](http://stackoverflow.com/questions/10990536/compilation-errors-with-imageresizer-3-2-1) – Lilith River Jun 15 '12 at 15:57
  • This error occurs if you need to target an old Framework version. Not sure how old, but I got it when I had to target .Net Framework 2.0. The above solution worked. – j2associates Jan 18 '18 at 16:14

16 Answers16

73

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

Community
  • 1
  • 1
7wp
  • 12,505
  • 20
  • 77
  • 103
  • 3
    If you are using Json.NET in a .NET 3.5 environment you should use the build for .NET 3.5 rather than 2.0 like you are currently. The 3.5 build doesn't include the ExtensionAttribute class. – James Newton-King Feb 16 '10 at 03:52
  • James You are right. I downloaded Json.Net's ZIP, and there was a 3.5 version. Thanks! – 7wp Feb 22 '10 at 19:57
  • 1
    +1 I had exactly the same issue. As a side note to James Newton-King, I also use Json.NET 2.0 with .NET 3.5, because on fairly modern versions of mono (e.g. stock build from Ubuntu 9.10), json.net depends on some odd DLLs that aren't supported yet. So I compile it with the older version of json.net and it's been working fine for many months. – Timothy Baldridge Apr 16 '10 at 13:53
  • 1
    +1 this was my problem too. I figured the newtonsoft.json.dll version of Json.NET was compiled against .net 1 – stimms May 17 '10 at 18:59
  • 1
    Man I just wasted an hour on this. Thanks for the answer. 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. – Charlie Flowers Oct 15 '10 at 07:07
  • @Charlie Flowers, Thanks for pointing that out too. I have updated the answer to give a heads-up to others in the same situation. – 7wp Oct 15 '10 at 16:12
  • I got same error when I convert VS2008 project to VS2010 (still .NET 3,5). My project is using Momo.Cecil.dll. I end up with remove and readd that Mono.Cecil dll to my reference list. – Ant Nov 19 '10 at 07:33
  • 1
    Exactly the same problem for me too in a .NET 4 project. By Default the NUGET package references the 2.0 version. Just remove that and add the .net 4 dll from NuGet's packages folder – MrBliz Apr 22 '11 at 13:50
  • NewtonSoft v2.0 doesn't work for me. Running .net 4.0 profile – Bamboo Jan 13 '12 at 09:45
  • 1
    @Bamboo As far as I know .Net 4.0 is not compatible with DLL files built for .Net 2.0 or 3.5 – 7wp Jan 13 '12 at 16:23
  • 1
    Thats for that answer. I realised I was using the 2.0 version for JSON.net in my 3.5 project, deleting and updating the reference sovled my problem. – MrJoeBlow Mar 29 '12 at 09:31
  • Is there a solution that doesn't involve multiple assemblies? [The ExtensionAttribute hack put me into a serious catch-22 situation](http://stackoverflow.com/questions/10990536/compilation-errors-with-imageresizer-3-2-1)... – Lilith River Jun 15 '12 at 15:59
  • The solution is to manually compile project with this issue, because Visual Studio is stupid enough to add custom assemblies as references to compiler's command line (first Newtonsoft.Json.Net20.dll and then System.Core.dll) instead of put BCL assemblies first. – mnn Jul 02 '12 at 11:55
  • Hey, Thanks for your post...i'm also facing the same problem, when i removed Newtonsoft.Json.Net20.dll reference and reinserted it into project...my project built successfully. – Chetan S Jul 19 '12 at 06:37
  • Chets: Yes, I've tried that several times (even restarted Visual Studio inbetween) - none of that works. Interesting is that this happened for me only when compiling Release version. – mnn Jul 21 '12 at 00:10
  • 3
    This should be the accepted answer. Newtonsoft.Json.Net20 was the culprit! – Levitikon Aug 13 '13 at 13:32
  • This issue was intermittent for me (closing the solution sometimes fixed it). I also got the warning `The predefined type 'System.Runtime.CompilerServices.ExtensionAttribute' is defined in multiple assemblies in the global alias; using definition from 'd:\...\Newtonsoft.Json.dll'`. Updating Newtonsoft to .NET 4.0 (which matched the rest of my solution) fixed the issue. – ErikusMaximus Apr 24 '18 at 14:09
21

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

abelenky
  • 63,815
  • 23
  • 109
  • 159
18

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 :-)

Michael Buen
  • 38,643
  • 9
  • 94
  • 118
17

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

Andrew Myhre
  • 1,444
  • 3
  • 18
  • 27
  • 1
    The link is broken... Also, this [hack puts you into a serious catch-22 situation](http://stackoverflow.com/questions/10990536/compilation-errors-with-imageresizer-3-2-1) – Lilith River Jun 15 '12 at 15:59
7

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

Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
3

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]

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

A missing System.Core reference will give these symptoms.

jyoung
  • 5,071
  • 4
  • 30
  • 47
2

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.

DJGray
  • 504
  • 8
  • 26
  • 1
    This was the case for me too. However, I didn't have to find any files, I just had to "add reference" and search "csharp" under "assemblies". There, I was able to select the mentioned reference and everything worked fine from then on. – Tobias Feil Apr 12 '19 at 09:32
1

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>
lexx
  • 637
  • 3
  • 9
0

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.

Gais
  • 565
  • 3
  • 10
  • 17
0

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.

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
0

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.

Gabriel Isenberg
  • 25,869
  • 4
  • 37
  • 58
0

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

Denis
  • 3,653
  • 4
  • 30
  • 43
0

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" />
Elya Livshitz
  • 404
  • 1
  • 6
  • 9
0

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

0

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

Hash
  • 821
  • 7
  • 19