C# 无法找到编译动态表达式所需的一种或多种类型。您是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11725514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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?
提问by user1232138
I am trying to compile this code in Microsoft Visual C# 2010
我正在尝试在 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
错误 1 未定义或导入预定义类型“Microsoft.CSharp.RuntimeBinder.Binder”
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?
错误 2 无法找到编译动态表达式所需的一种或多种类型。您是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?
I read this other postbut I am new to C# and I couldn't understand what really is the problem. Especially what and where are these so called .configfiles..
我阅读了另一篇文章,但我是 C# 的新手,我不明白真正的问题是什么。特别是这些所谓的.config文件是什么以及在哪里。
采纳答案by M. Mennan Kara
On your solution explorer window, right click to References, select Add Reference, go to .NET tab, find and add Microsoft.CSharp.
在您的解决方案资源管理器窗口中,右键单击引用,选择添加引用,转到 .NET 选项卡,找到并添加 Microsoft.CSharp。
回答by Chris Shain
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 dynamickeyword.
确保您的项目面向 .NET Framework 4.0。Visual Studio 2010 也支持 .NET 3.5 框架目标,但 .NET 3.5 不支持dynamic关键字。
You can adjust the framework version in the project properties. See http://msdn.microsoft.com/en-us/library/bb398202.aspxfor more info.
您可以在项目属性中调整框架版本。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb398202.aspx。
回答by Roman
Red lines under the ViewBag was my headache for 3 month ). Just remove the Microsoft.CSharp reference from project and then add it again.
ViewBag 下的红线是我头疼的 3 个月)。只需从项目中删除 Microsoft.CSharp 引用,然后再次添加它。
回答by chan
I had the same problem and solved it by removing "Microsoft.CSharp" reference from the project and then added it again.
我遇到了同样的问题,并通过从项目中删除“Microsoft.CSharp”引用来解决它,然后再次添加它。
回答by dynamiclynk
I had the same issue except removing and adding the reference back did not fix the error, so I changed .Net version from 4.5to 4.5.1.
我有同样的问题,除了删除和添加参考后面没有修复的错误,所以我从改变.NET版本4.5来4.5.1。
To achieve this go to your web.configfile and change the following lines
要实现这一点,请转到您的web.config文件并更改以下几行
<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.
并重建。
回答by Ramkumar Thangavel
If you miss, Microsoft.CSharp.dll this error can occur. Check you project references.
如果错过,Microsoft.CSharp.dll 可能会发生此错误。检查您的项目参考。
回答by Mick
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.
我的类库肯定都引用了 System.Core 和 Microsoft.CSharp。Web 应用程序是 4.0,由于支持问题无法升级到 4.5。
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.
我在使用Razor Engine编译剃刀模板时遇到错误,并且只是间歇性地遇到它,比如在重新启动 Web 应用程序之后。
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.
希望这可以帮助其他人。
回答by Mellow
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.
对我来说,删除并重新添加对 Microsoft.CSharp 的引用可以暂时解决该问题,直到编辑受影响的文件。关闭 Visual Studio 并重新打开项目可以更长期地修复它,因此如果在 Microsoft.CSharp 已被引用的情况下发生这种情况,这是一个选项。
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.
也许作为第一步重新启动 IDE 似乎微不足道,但对于像我这样不认为这是第一件事的人,这里有一个提醒。

