6

I recently installed the Visual Studio 11 Beta, and I'm trying to update an existing 4.0 project to use 4.5. In the program it compiles some dynamically generated code using 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;
    }
}

The problem comes when I changed the CompilerVersion from { "CompilerVersion", "v4.0" } to { "CompilerVersion", "v4.5" }

I now get an exception

Compiler executable file csc.exe cannot be found.

Is specifying CompilerVersion the wrong way to tell it to use 4.5? Will compiling it as v4.5 even make a difference since the code won't be using any new 4.5 specific features?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
BrandonAGr
  • 5,827
  • 5
  • 47
  • 72
  • 2
    I don't know if it is wrong to specify the compiler version like that but FWIW the compiler in .NET 4.5 is still version 4.0 according to the output. – Brian Rasmussen Mar 06 '12 at 20:48
  • Why do you return the first error only? – abatishchev Mar 07 '12 at 17:28
  • @abatishchev Since the code I'm compiling is all autogenerated there typically shouldn't be any errors unless the template is messed up, just returning the first prevents it from returning possibly thousands of errors, which would just result in a really long error email and not really be anymore helpful – BrandonAGr Mar 07 '12 at 17:45

1 Answers1

12

It would have helped if you'd given us a short but complete program to demonstrate the problem, but I can verify that with "v4.0" it will work and compile async methods, assuming you're running on a machine with the VS11 beta installed.

Demonstration:

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);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Is it still version 4.0 because 4.5 is still in beta? Is the CompilerVersion going to be bumped in the final version? Also, is CompilerVersion the framework version or the C# compiler version? – BFree Mar 06 '12 at 21:16
  • Thanks, it looks like since 4.5 installs on top of 4 and has the same version number I don't need to change the CompilerVersion in order to use 4.5 – BrandonAGr Mar 06 '12 at 21:26
  • @BFree: Wouldn't like to say, to be honest. – Jon Skeet Mar 06 '12 at 21:31