使用 CSharpCodeProvider 时出现运行时错误程序集 'System.Runtime'

getting runtime error assembly 'System.Runtime' while using CSharpCodeProvider

我收到有关缺少引用的运行时错误。

The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

我正在使用 MVC 应用程序并在我的代码中使用了 CSharpCodeProvider。

我没有收到任何编译错误,但收到 compileResult 的运行时错误,为什么?

 CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

我什至在 web.cofig 中添加了 assemblies 标签,就像以下仍然相同的 error.any 线索为什么?

  <assemblies>     
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />   
  </assemblies>

ASP.NET MVC 经常使用 dynamic 关键字。这可能是问题的根源,因为这需要 Microsoft.CSharp 程序集(您显然已经包括在内)和 System.Runtime 程序集(我认为缺少这个。

在编译配置中添加 System.Runtime 程序集:

parameters.ReferencedAssemblies.Add("mscorlib.dll"); // guess you have this one already
parameters.ReferencedAssemblies.Add("System.Runtime.dll");