Fixing the C# compiler error in legacy ASP.NET MVC applications that don’t support using the dynamic ViewBag object

Posted: (EET/GMT+2)

 

I was today working on an older ASP.NET MVC application, which I wanted to convert to the latest library versions and coding standards. For one thing, I wanted to start using the C#'s dynamic type "ViewBag", instead of using the older ViewData dictionary.

However, when I tried using ViewBag in newer code, Visual Studio stopped to the following error during build:

Severity: Error

Code: CS0656

Description: Missing compiler required member

'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'.

The error message is somewhat vague, and does not directly point into the correct direction. However, in this case, you have to look for clues in the name of the class CSharpArgumentInfo: since a member of this class is missing, it's likely the compiler is not able to find the class.

Armed with this knowledge, the solution becomes simple: make sure you refer to the assembly "Microsoft.CSharp" in your project. After I added the reference to this assembly (version 4.0, by default located at "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll"), the build error went away.

Problem solved!