c# - 在 .net 4.5 beta 中使用 CSharpCodeProvider

标签 c# compiler-construction .net-4.5 csharpcodeprovider

我最近安装了 Visual Studio 11 Beta,我正在尝试更新现有的 4.0 项目以使用 4.5。在程序中,它使用 CSharpCodeProvider 编译一些动态生成的代码。 .

/// <summary>
/// Compile and return a reference to the compiled assembly
/// </summary>
private Assembly Compile()
{
    var referencedDlls = new List<string>
    {
        "mscorlib.dll",
        "System.dll",
        "System.Core.dll",
    };
    referencedDlls.AddRange(RequiredReferences);

    var parameters = new CompilerParameters(
        assemblyNames: referencedDlls.ToArray(),
        outputName: GeneratedDllFileName,
        // only include debug information if we are currently debugging
        includeDebugInformation: Debugger.IsAttached);
    parameters.TreatWarningsAsErrors = false;
    parameters.WarningLevel = 0;
    parameters.GenerateExecutable = false;
    parameters.GenerateInMemory = false;
    parameters.CompilerOptions = "/optimize+ /platform:x64";

    string[] files = Directory.GetFiles(GenerationDirectory, "*.cs");

    var compiler = new CSharpCodeProvider(
        new Dictionary<string, string> { { "CompilerVersion", "v4.5" } });
    var results = compiler.CompileAssemblyFromFile(parameters, files);

    if (results.Errors.HasErrors)
    {
        string firstError =
            string.Format("Compile error: {0}", results.Errors[0].ToString());
        throw new ApplicationException(firstError);
    }
    else
    {
        return results.CompiledAssembly;
    }
}

当我将 CompilerVersion{ "CompilerVersion", "v4.0"} 更改为 { "CompilerVersion", "v4.5 "

我现在得到一个异常(exception)

Compiler executable file csc.exe cannot be found.

指定 CompilerVersion 是否是告诉它使用 4.5 的错误方式?由于代码不会使用任何新的 4.5 特定功能,将其编译为 v4.5 甚至会有所不同吗?

最佳答案

如果您给我们一个简短但完整的程序来演示问题,那将会有所帮助,但我可以验证“v4.0”它是否可以工作并编译异步方法,假设您在机器上运行安装了 VS11 测试版。

演示:

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

namespace Foo {}

class Program
{
    static void Main(string[] args)
    {
        var referencedDll = new List<string>
        {
            "mscorlib.dll",
            "System.dll",
            "System.Core.dll",
        };

        var parameters = new CompilerParameters(
             assemblyNames: referencedDll.ToArray(),
             outputName: "foo.dll",
             includeDebugInformation: false)
        {
            TreatWarningsAsErrors = true,
            // We don't want to be warned that we have no await!
            WarningLevel = 0,
            GenerateExecutable = false,
            GenerateInMemory = true
        };

        var source = new[] { "class Test { static async void Foo() {}}" };

        var options = new Dictionary<string, string> {
             { "CompilerVersion", "v4.0" }
        };
        var compiler = new CSharpCodeProvider(options);
        var results = compiler.CompileAssemblyFromSource(parameters, source);

        Console.WriteLine("Success? {0}", !results.Errors.HasErrors);
    }
}

关于c# - 在 .net 4.5 beta 中使用 CSharpCodeProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591584/

相关文章:

c++ - 为什么 C++ 中的 ClassName ClassName 变量定义可以正常编译和工作?

c# - 编译器歧义错误中的不一致

c# - 如何使用某些脚本为 wpf 4.5 应用程序创建安装包

c# - 无法让 ApplyCurrentValues(Entity) 在 Entity Framework 5 中工作

c# - 创建一个 Txt 文件并写入

c# - Xamarin 表单选择器绑定(bind)

c# - 流利的断言 : How to break through drill-down of Which/And cascades?

c# - 根据特定条件选择用户(如果列表中存在)

c - token 是在预处理之后计数还是仅在预处理过程中计数?

c# - 二进制序列化和自动属性