102

Today after deploying some changes to a C# MVC site that I run, I went back to make some more modifications and came across this error:

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

The error is a bit vague (other than it's description, obviously) as it doesn't give me a file, line, or column to reference, only the project. Also, it throws the error a total of 20 times. I only made three changes to the code between the time I deployed (it was completely functional at that time) and now. I reverted my changes and it is still throwing the same error which makes no sense to me.

I haven't found a lot of information on this error on SO or Google, other than this guys solution and a couple references to some Mono project errors (I'm not using Mono). The solution the guy above gives requires adding a class definition that will allow the compiler to resolve the reference. I don't particularly want to do this because I haven't needed to do it up until this point and it will just muddy my code.

Just curious if anyone has run across this before.

starball
  • 20,030
  • 7
  • 43
  • 238
jamesmillerio
  • 3,154
  • 4
  • 27
  • 32
  • 1
    Seems like you're using a C# 3.0 compiler but compiling against the .NET Framework 2.0. Check your assembly references. – dtb May 31 '11 at 03:15

12 Answers12

245

In my case it was because the project was not referencing Microsoft.CSharp. Once I added a reference to that assembly, it compiled just fine.

Mike
  • 7,500
  • 8
  • 44
  • 62
  • 3
    Solved the problem. – Anton Lyhin Nov 18 '16 at 20:47
  • 17
    My problem was in a Unit Test using dynamic and Expando object. Referencing Microsoft.CSharp was the trick. Thanks! – realPT May 03 '17 at 18:48
  • 2
    I had dynamic type NewtonSoft etc In .NetStandard Class library: ``` string weatherjson = await GetAsync(url); dynamic obj = JsonConvert.DeserializeObject(weatherjson); dynamic temp = obj.main.temp; ``` I did Nuget Search for Microsoft.CSharp and installed that. Library now builds :) Q. Can I use this .NetStandard library in Linux app, given the Microsofct reference? – David Jones Aug 27 '19 at 12:10
  • Thank you! For some reason this was not obvious to me from the message! I am tweaking a project written long ago and this reference stuff is frightening. Makes one appreciate the relative clarity of Java which I normally use. – Tuntable Feb 12 '20 at 11:08
  • solved the issue – PPr Jan 25 '22 at 13:06
88

I don't know if anyone else has experienced this, but I suddenly ran into this error after adding some code utilizing dynamic types and incorporating WebAPI into a project that originated as a TypeScript application in VS2013. Simply adding a reference to Microsoft.CSharp resolved my issue.

Hope this helps someone else.

H Boyce
  • 1,073
  • 8
  • 15
27

This error usually means either your project is compiling against .NET 2.0 or you aren't referencing the correct version of System.Core.dll

For a near duplicate question, see Error when using extension methods in C#

Community
  • 1
  • 1
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • It actually turned out that I had a bad reference from an assembly that I was no longer using (Azure). I don't think the Azure assembly was compiled against .Net 2.0 but removing it fixed the issue. I'm not sure why this randomly came into being as I haven't touched anything related to my assemblies or .Net version (4.0) in quite some time. Alas, thanks for pointing me in the right direction. – jamesmillerio May 31 '11 at 04:18
  • Unity3D sets the compiler to .NET 2 by default (at time of writing). In player settings, change it to the newer.NET version to fix this issue. – Shadow Jul 13 '20 at 03:56
17

I ran into this situation as well today. In my case I was referencing the Newton.Json.Net dll v3.5 in my .NET 4.0 application. I realized that I wasnt even using this library, thus once I removed it from my references, it no longer gave me the compiler error.

Problem solved!!!

Bat_Programmer
  • 6,717
  • 10
  • 56
  • 67
7

The actual error comes from the fact that your 2.0 assembly that causes the error contains this code:

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

The Code Above allows the .NET 2.0 Assembly to use extension methods (see Using extension methods in .NET 2.0?). While it confuses the compiler if you target .NET 4.0 and reference a 2.0 Assembly (containing above code) as the mscorlib.dll(4.0) contains the same class in the same namespace.

I fixed this

  • by compiling the original 2.0 assembly again without the attribute targeting 4.0
  • by removing the assembly (obviously)
  • by adding a third Extension Attribute in the target you compile (it seems to overrule the referenced definitions)
Community
  • 1
  • 1
quadroid
  • 8,444
  • 6
  • 49
  • 82
6

Writing this code somewhere in your project may solve your problem. It works for me

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute { }
}
AlexB
  • 7,302
  • 12
  • 56
  • 74
  • where you write this code? you created a class in root of project? – Alex Nov 18 '15 at 11:16
  • 1
    @Alex I added a class called CompilerService to the root of my project with the code aboce and it worked. Nice! – Halvard Mar 11 '16 at 12:33
6

Probably you use dynamic keyword In .NetStandard Class library project. If so, you need to add a reference to Microsoft.CSharp library in the project. Hope it will resolve your problem.

Towhidul Islam Tuhin
  • 1,051
  • 13
  • 10
4

NLog.dll 2.0 referenced from a .NET 4.0 project can cause this too.

Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148
2

I don't have a correct solution, but I'll add my data point:

In my case the error is caused by referencing GoogleSearchAPINet20

Here is what happens:

  • I close the solution that builds
  • I open the solution again. It still builds
  • As soon as I make any change and try to build, I get 19 "Missing compiler required member ..." errors
  • I remove the reference to GoogleSearchAPINet20
  • I add back the reference to GoogleSearchAPINet20
  • I build the solution. It builds without errors
  • I can now make code changes, build or perform any other actions with solution correctly as long as my Visual Studio is open
  • I close Visual Studio
  • Repeat from step one

I'm not referencing System.Core.dll in my solution at all and my target framework is .NET 4.

I'm a bit annoyed at this point ...

Evgeny
  • 3,320
  • 7
  • 39
  • 50
1

Got this error when trying to use async Tasks against .NET 4.0. Updating Target Framework to 4.5.2 fixed the problem.

Eternal21
  • 4,190
  • 2
  • 48
  • 63
1

I hit the same set of exceptions after I added some async methods to a winforms project. I needed to bump my .NET version from 4 to 4.5

user8675309
  • 591
  • 1
  • 6
  • 24
0

For me, the problem occure when I add asynchronous method with async Task await in my .net4.0 project !

With previous versions of the .NET-Framework 4.5, you must install this package:

Install-package Microsoft.Bcl.Async –pre

or

Install-Package Microsoft.CompilerServices.AsyncTargetingPack

more info on Nuget or Nuget

A. Morel
  • 9,210
  • 4
  • 56
  • 45