如何使用CSharpCodeProvider定位.net 4.5?

时间:2012-11-06 15:15:57

标签: c# reflection .net-4.5

  

可能重复:
  Using CSharpCodeProvider with .net 4.5 beta

对于.net 3.5我将v3.5传递给CSharpCodeProvider,当我在v4.5应用程序中将v4.5传递给CSharpCodeProvider时,我得到InvalidOperationException“无法找到编译器可执行文件csc.exe。” p>

任何人都知道这里发生了什么,我做错了什么?

重现的代码。 。

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace Console1
{
    class Program
    {
        static void Main(string[] args)
        {
            var options = new Dictionary<string, string>{{"CompilerVersion", "v4.5"}};
            var cs = new CSharpCodeProvider(options);

            var compilerParams = new CompilerParameters();

            var r = cs.CompileAssemblyFromSource(compilerParams , "namespace ns { class program { public static Main(string[] args) { System.Console.WriteLine(\"Hello world\"); } } }");
        }
    }
}

1 个答案:

答案 0 :(得分:27)

这是设计使用,当您使用Windows资源管理器导航到c:\ windows \ microsoft.net \ framework时,您可以看到这些内容。请注意,您只会看到名为v4.0.30319的子目录,没有v4.5子目录。或者换句话说,.NET 4.5是版本4.0的真正就地更新,而C#v5编译器替换了 v4编译器。

您需要指定“v4.0”。