views:

4034

answers:

17

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?

+1  A: 

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
+2  A: 

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

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.

DrJokepu
A: 

I have the target framework set to 3.5 in the project properties.

Korbin
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
(meaning: not System.Core.dll, and perhaps with an internal constructor)
Marc Gravell
A: 

Are you sure you are referencing the correct version of System.Core.dll?

DrJokepu
A: 

Yup. When I look under references an right click on System.Core and go to properties, it says "3.5.0.0" in the version column.

Korbin
A: 

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

Yeah I tried that, but here is the interesting thing. I just created the same static class with the extension method in the web project and no compile errors. It only fails to compile in a MS Unit Test project.

I'm gonna try the same test in a standard class project using MbUnit to see if this is a limitation of the MS Unit Tests Project.

Korbin
A: 

Are you able to reference any other .NET 3.5 types? Can you call Enumerable.Range, for example?

Can you compile short but complete programs from the command line and introduce extension methods there?

Jon Skeet
A: 

A missing System.Core reference will give these symptoms.

jyoung
+4  A: 

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
+1 Merry Christmas
Corin
+10  A: 

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
I had this problem and this was the only solution that worked for me (inside VS).
Mario
Definitely strange, but it worked for me as well! Can't argue with that, thank you!
billb
A: 

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

A: 

Thank you Michael. This was the problem in my case :)

+1  A: 

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

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
+18  A: 

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

Roberto Sebestyen
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
James You are right. I downloaded Json.Net's ZIP, and there was a 3.5 version. Thanks!
Roberto Sebestyen
Yep, this was the solution for me to!
David
+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
+1 this was my problem too. I figured the newtonsoft.json.dll version of Json.NET was compiled against .net 1
stimms
THIS! ^^^^^^^^^
Andrew Bullock
@andrew, what??
Roberto Sebestyen
this is the answer! ;)
Andrew Bullock
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
@Charlie Flowers, Thanks for pointing that out too. I have updated the answer to give a heads-up to others in the same situation.
Roberto Sebestyen