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

42,971

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

Solution 2

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]

Solution 3

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.

enter image description here

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.

Solution 4

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

Solution 5

Saw this in VS 2019. I excluded and re-included the problematic file (in my case _Layout.cshtml) and that fixed the problem.

Share:
42,971

Related videos on Youtube

Alejandro Lozdziejski
Author by

Alejandro Lozdziejski

Updated on November 12, 2020

Comments

  • Alejandro Lozdziejski
    Alejandro Lozdziejski over 3 years

    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:

    enter image description here

    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?

    • DavidG
      DavidG almost 9 years
      What version of .Net Framework are all of the projects set to use?
    • Alejandro Lozdziejski
      Alejandro Lozdziejski almost 9 years
      David, All projects are targeting 4.5 framework (Project -> properties -> Application -> Target framework)
    • code4life
      code4life over 8 years
      I think this link: stackoverflow.com/questions/18944208/… answers your question...
    • Michał Komorowski
      Michał Komorowski over 8 years
      Could you give us a link to the problematic GitHub project?
    • Simon Mourier
      Simon Mourier over 8 years
      Could you examine the full compilation error (the yellow image is only partial), to determine what's the CSC.EXE full command line (references are r:/ arguments, etc.)
    • Tamas Ionut
      Tamas Ionut over 8 years
      Please show us the code or the Github project link, so we can test it ourselves. Most likely, you are somehow targeting a wrong NET Framework
    • Rajshekar Reddy
      Rajshekar Reddy over 8 years
      whats the link to github project
    • jitendra singh
      jitendra singh over 8 years
      I understand that your project targets (or >) to .net 4; but there must be one library that targets one of the "old" frameworks.
    • Dennis Nerush
      Dennis Nerush over 8 years
      What VIsual Studio do you use? Please post the link to the Github project. I guess that it's code is targeting .NET 4.5.2 or above.
    • Jay
      Jay about 8 years
      Hey guys, Any idea how to solve this? tried below answer by targeting a different version But I got more errors so had to roll back, Currently targeting 4.5 and was getting the error on that. Please help.
    • tchelidze
      tchelidze almost 8 years
      Restarting Visual Studio worked for me.
  • naveen
    naveen almost 8 years
    update 3 is giving me enough headaches too. thanks for the info
  • DaVinciCoder
    DaVinciCoder over 7 years
    This worked for me but I needed to close my solution before I rebuilt it in the earlier framework.
  • Leonardo Cunha
    Leonardo Cunha about 6 years
    This worked for me. VS 2015. Changed from framework 4.6 back to 4.5.2 and back to 4.6. Nice. Thanks.
  • Paramone
    Paramone about 6 years
    Worked for me aswell with .NET 4.6.2. Couldn't use linq statements on (casted) IENumerables.
  • colidyre
    colidyre over 5 years
    This does not make any new contribution in respect to existing answers.
  • Akash Mishra
    Akash Mishra over 5 years
    this is not a new contribution but this is very clear and working solution for this error.
  • WHol
    WHol over 5 years
    I can't believe this worked... I had a couple other nuget packages doing the same thing. The views would run mostly but the Error List in Visual Studio was full of annoying errors.
  • Marc Roussel
    Marc Roussel almost 5 years
    I tried targeting all frameworks from 4 to 4.7 and none of these re-targeting worked for me.
  • Marc Roussel
    Marc Roussel almost 5 years
    I tried the complete solution here witch took a very long time by the way and to no luck, The error is still there in a .cshtml file opened.
  • James Wilkins
    James Wilkins almost 5 years
    That was back in 2016. There could be a lot of differences today depending on the project types, references, VS version, etc. For instance, I was using 2015 back then. Today I'm using 2017.
  • Marc Roussel
    Marc Roussel almost 5 years
    I'm using 2019 latest here. Ok it's solved. Somehow, I was missing the assembly System.Core in the Web.Config. After adding again, closing the solution and opening again, the message was gone. Even thought the System.Dynamic.Runtime wasn't referenced. I think the Web.Config contains the link which is taken in System.Core but I'm not sure. All in all, adding the assembly System.Core under <compilation><assemblies></assemblies></compilation> is enough
  • BVernon
    BVernon about 4 years
    Same solution for me.
  • Miss Chanandler Bong
    Miss Chanandler Bong about 4 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • Ron Swanson
    Ron Swanson about 4 years
    I disagree with you on that. My solution worked in my case and was offered as an option/possibility for anyone who might stumble upon this thread via a Google search like I did. If my solution is wrong, so is brichins' which is basically the same thing and I don't see you nagging that person.
  • Adam R. Turner
    Adam R. Turner about 3 years
    In development we often face red herring errors, especially when compiling. This is one of those cases for many who see this error I suspect. This actually does fix the problem, even if it works indirectly.