How can I target a specific language version using CodeDOM?

久未见 提交于 2019-11-30 08:44:27

You need to instruct the C# compiler (that CSharpCodeProvider uses indirectly) that you want to link to another mscorlib.dll, using the /nostdlib option. Here is a sample that should do it:

static void Main(string[] args)
{
    // defines references
    List<string> references = new List<string>();

    // get a reference to the mscorlib you want
    var mscorlib_2_x86 = Path.Combine(
                         Environment.GetFolderPath(Environment.SpecialFolder.Windows),
                         @"Microsoft.NET\Framework\v2.0.50727\mscorlib.dll");
    references.Add(mscorlib_2_x86);

    // ... add other references (System.dll, etc.)

    var provider = new CSharpCodeProvider(
                   new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
    var parameters = new CompilerParameters(references.ToArray(), "program.exe");
    parameters.GenerateExecutable = true;

    // instruct the compiler not to use the default mscorlib
    parameters.CompilerOptions = "/nostdlib";              

    var results = provider.CompileAssemblyFromSource(parameters,
        @"using System;

        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(""Hello world from CLR version: "" + Environment.Version);
            }
        }");
}

If you run this, it should compile a program.exe file. If you run that file, it should display something like this:

Hello world from CLR version: 2.0.50727.7905
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!