如何使用CSharpCodeProvider定位.net 4.5?

Bin*_*ier 19 c# reflection .net-4.5

可能重复:
使用带有.net 4.5 beta的CSharpCodeProvider

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

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

代码重现...

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\"); } } }");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 29

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

您需要指定"v4.0".