“缺少编译器所需成员”错误被多次抛出,几乎没有更改代码

今天,在将一些更改部署到我运行的 C # MVC 站点之后,我返回做了一些更多的修改,发现了这个错误:

缺少编译器所需的成员 System.Runtime.CompilerServices.ExtensionAttribute. . ctor

这个错误有点模糊(显然,除了它的描述) ,因为它没有给我一个文件、行或列来引用,只有项目。此外,它总共抛出20次错误。从部署到现在,我只对代码进行了三次更改(当时它是完全可用的)。我恢复了我的更改,但它仍然抛出同样的错误,这对我来说毫无意义。

除了 这家伙解决方案和一些 Mono 项目错误(我没有使用 Mono)之外,我没有在 SO 或 Google 上找到关于这个错误的很多信息。上面提供的解决方案需要添加一个类定义,这个类定义允许编译器解析引用。我并不特别想这么做,因为直到现在我都不需要这么做,这只会让我的代码变得混乱。

只是好奇是否有人以前遇到过这个。提前感谢!

76827 次浏览

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#

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!!!

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 ...

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

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

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

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)

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

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.

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.

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

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

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.