我试图在Microsoft Visual c# 2010中编译此代码
using System;
using System.Globalization;
class main
{
static void Main()
{
dynamic d;
d = "dyna";
Console.WriteLine(d);
}
}
但是我得到了这两个错误
错误1预定义类型“Microsoft.CSharp.RuntimeBinder”。没有定义或导入Binder
错误2无法找到编译动态表达式所需的一个或多个类型。您是否缺少对Microsoft.CSharp.dll和System.Core.dll的引用?
我读了另一篇文章,但我是c#的新手,我不明白真正的问题是什么。特别是这些所谓的。config文件是什么,在哪里。
这些对我都没用。
我的类库肯定都引用了System。酷睿和微软。csharp。Web应用程序是4.0,由于支持问题无法升级到4.5。
我遇到了使用剃须刀引擎编译剃须刀模板的错误,而且只是断断续续地遇到它,就像在web应用程序重新启动后。
对我有效的解决方案是手动加载程序集,然后重新尝试相同的操作……
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;
}
}
希望这能帮助到其他人。