One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

C#.Net

C# Problem Overview


I am trying to compile this code in Microsoft Visual C# 2010

using System;
using System.Globalization;


class main
{
    static void Main()
    {

        dynamic d;
        d = "dyna";
        Console.WriteLine(d);    
    }
}

but I am getting these two errors

Error 1 Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported

Error 2 One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

I read this other post but I am new to C# and I couldn't understand what really is the problem. Especially what and where are these so called .config files..

C# Solutions


Solution 1 - C#

On your solution explorer window, right click to References, select Add Reference, go to .NET tab, find and add Microsoft.CSharp.

Alternatively add the Microsoft.CSharp NuGet package.

Install-Package Microsoft.CSharp

Solution 2 - C#

Make sure that your project is targeting the .NET framework 4.0. Visual Studio 2010 supports .NET 3.5 framework target also, but .NET 3.5 does not support the dynamic keyword.

You can adjust the framework version in the project properties. See http://msdn.microsoft.com/en-us/library/bb398202.aspx for more info.

Solution 3 - C#

I had the same issue except removing and adding the reference back did not fix the error, so I changed .Net version from 4.5 to 4.5.1.

To achieve this go to your web.config file and change the following lines

<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />

to this

<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />

and rebuild.

Solution 4 - C#

Red lines under the ViewBag was my headache for 3 month ). Just remove the Microsoft.CSharp reference from project and then add it again.

Solution 5 - C#

I had the same problem and solved it by removing "Microsoft.CSharp" reference from the project and then added it again.

Solution 6 - C#

For me, removing and re-adding a reference to Microsoft.CSharp fixed the problem temporarily until the affected file was edited. Closing Visual Studio and reopening the project fixed it more long-term, so that's an option if this situation occurs while Microsoft.CSharp is already referenced.

Maybe restarting the IDE as a first step seems trivial, but here's a reminder for people like me who don't think of that as the first thing to do.

Solution 7 - C#

None of these worked for me.

My class libraries were definitely all referencing both System.Core and Microsoft.CSharp. Web Application was 4.0 and couldn't upgrade to 4.5 due to support issues.

I was encountering the error compiling a razor template using the Razor Engine, and only encountering it intermittently, like after web application has been restarted.

The solution that worked for me was manually loading the assembly then reattempting the same operation...

        bool retry = true;
        while (retry)
        {
            try
            {
                string textTemplate = File.ReadAllText(templatePath);
                Razor.CompileWithAnonymous(textTemplate, templateFileName);
                retry = false;
            }
            catch (TemplateCompilationException ex)
            {
                LogTemplateException(templatePath, ex);
                retry = false;

                if (ex.Errors.Any(e  => e.ErrorNumber == "CS1969"))
                {
                    try
                    {
                        _logger.InfoFormat("Attempting to manually load the Microsoft.CSharp.RuntimeBinder.Binder");
                        Assembly csharp = Assembly.Load("Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                        Type type = csharp.GetType("Microsoft.CSharp.RuntimeBinder.Binder");
                        retry = true;
                    }
                    catch(Exception exLoad)
                    {
                        _logger.Error("Failed to manually load runtime binder", exLoad);
                    }
                }

                if (!retry)
                    throw;
            }
        }

Hopefully this might help someone else out there.

Solution 8 - C#

If you miss, Microsoft.CSharp.dll this error can occur. Check you project references.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1232138View Question on Stackoverflow
Solution 1 - C#M. Mennan KaraView Answer on Stackoverflow
Solution 2 - C#Chris ShainView Answer on Stackoverflow
Solution 3 - C#dynamiclynkView Answer on Stackoverflow
Solution 4 - C#RomanView Answer on Stackoverflow
Solution 5 - C#Mr JernbergView Answer on Stackoverflow
Solution 6 - C#MellowView Answer on Stackoverflow
Solution 7 - C#MickView Answer on Stackoverflow
Solution 8 - C#Ramkumar ThangavelView Answer on Stackoverflow