如何使用 CSharpCodeProvider 定位 .net 4.5? [英] How to target .net 4.5 with CSharpCodeProvider?

查看:34
本文介绍了如何使用 CSharpCodeProvider 定位 .net 4.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在 .net 4.5 beta 中使用 CSharpCodeProvider

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

For .net 3.5 I pass v3.5 to CSharpCodeProvider, when I pass v4.5 to CSharpCodeProvider in a v4.5 app I get InvalidOperationException "Compiler executable file csc.exe cannot be found."

有人知道这里发生了什么吗,我做错了什么?

Anyone any idea what's going on here, what am I doing wrong?

代码重现...

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"); } } }");
        }
    }
}

推荐答案

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

This is by design, something you can see when you navigate to c:windowsmicrosoft.netframework with Windows Explorer. Note that you'll only see a subdirectory named v4.0.30319, there is no v4.5 subdirectory. Or in other words, .NET 4.5 is a true in-place update for version 4.0 and the C# v5 compiler replaces the v4 compiler.

您需要指定v4.0".

You'll need to specify "v4.0".

这篇关于如何使用 CSharpCodeProvider 定位 .net 4.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆