Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute'

倖福魔咒の 提交于 2019-12-20 10:59:37

问题


I'm trying to run a .NET MVC application on my local computer that I got from GitHub.

When I hit run on Visual Studio, everything complies and a new browser window opens with the error:

CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute'

The compiler section in the same window, shows the following error:

I've checked on google and this seems to be the same error.

This guy was using a dynamic type himself, on the other hand I'm not using any dynamic type, this is being auto generated by .NET's compiler, and I'm using .net 4.5 which should has support for dynamics.

I tried to apply the same solution (Changing all System.Core references to 4.0) but the thing is that all of them are version 4.0 already.

Do you have any idea how could I resolve this?


回答1:


I had a similar error on a project I was trying to migrate. Try re-targeting the framework of the project back a version or two, and once you find a target version where you do not get the error, re-target back to the version you originally had trouble with.

I was getting the same error on a website project targeted for 4.6.2 that was referencing some older libraries, re-targeted it back to 4.5.1 and that resolved the error. I immediately re-targeted back to 4.6.2 and did not encounter the error again.




回答2:


I got this error suddenly a couple days after updating to VS2015.3 using 'traditional' MVC app (not new .NET Core / vNext app).

Turns out this error can be a complete red herring.

Make sure:

  • You haven't got any other errors at all
  • You've checked the output window for any errors that don't show in the 'Output' window.
  • Make sure you don't have any conflicting references

In my case I had a typescript error that was causing an error and for some reason triggering this weird 'red herring' dynamic error too.

My fix was to go into the .csproj file and update the typescript tools version to 1.8 (need to unload project and edit by hand). As I said this is unrelated to the error, but I mention the specifics because someone else may end up with this same problem.

I believe that 1.7 didn't accept 'None' for the typescript module system whereas 1.8 does, hence the error.




回答3:


I had this same issue, but none of the answers here solved it, but did point me in the right direction. I think I didn't update the .NET version on my machine to the newer one. After installing it, I rolled back the targeted framework (as was suggested), retargeted the newer framework, and did a few extra things also:

  1. Reinstalled ALL packages by running this in the package manager:

    update-package -reinstall

I also compared my project to one that was working for 4.5 and did this:

  1. Opened my project file and removed all "BCL" related elements.
  2. Uninstalled all "BCL" packages in the NuGet package manager for the project (guess it's not needed moving to 4.5+...?)
  3. Removed an old package version of System.Net.Http, which did not get updated for some reason (see web.config - versions should show oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0").
  4. Have to restart VS.
  5. Rebuild solution.
  6. Error is now gone. ;)

One other thing I did to resolve some other errors was to clear the component cache by deleting all files in here:

C:\Users\{USERNAME}\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache\

[rant] Wasted a whole day with all this. Thanks to MS for stealing one more day of my life fixing senseless issues that should never be. Every time I upgrade I always get a wonderful surprise, and a lovely game of fix the red herring. Would it not hurt to detect these conflicts for those upgrading old projects? Perhaps better error traces when things go wrong? I doubt it would be that difficult. Perhaps even something like AsmSpy, but more advanced, built in to VS? [/rant]




回答4:


Adding a reference to System.Dynamic.Runtime did it for me.




回答5:


I had the same error, there were just 2 unused tables of my databaseset that I had to delete. I wonder why it worked when I manually change sth in an auto-generated code work but after deleting, it worked.

So be aware that such inconsistencies can also cause this error.




回答6:


This error will come because of the different versions of your Sitecore.Web.Mvc.Dll in solution reference folder(Project name->References->Sitecore.Web.Mvc->right click and check properties) and the version of Sitecore.Web.Mvc.Dll in Web.Config in views Folder.

For solving this error you have to make sure that the version of Sitecore.Web.Mvc.Dll is same in both folders.

Thanx.




回答7:


My solution was to correct the version of MVC in web.config under Views folder. E.g. the project referenced MVC assembly version 5.2.2.0 but in the Views/web.config file was version 5.2.4.0 (see image).

Image




回答8:


For my case, I fixed to run the command line "update-package -reinstall" on the Package Manager Controler.



来源:https://stackoverflow.com/questions/31761424/cannot-define-a-class-or-member-that-utilizes-dynamic-because-the-compiler-req

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!