Error when using extension methods in C#

48,860

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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

Share:
48,860
user1790201
Author by

user1790201

Updated on July 08, 2022

Comments

  • user1790201
    user1790201 almost 2 years

    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?