One or more types required to compile a dynamic ex

2020-01-23 16:52发布

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# .net
8条回答
Fickle 薄情
2楼-- · 2020-01-23 17:30

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-01-23 17:33

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

查看更多
forever°为你锁心
4楼-- · 2020-01-23 17:34

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

查看更多
我只想做你的唯一
5楼-- · 2020-01-23 17:35

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.

查看更多
够拽才男人
6楼-- · 2020-01-23 17:36

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

查看更多
劫难
7楼-- · 2020-01-23 17:38

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

查看更多
登录 后发表回答