How can I compile C # 7 code on the fly?

I have a simple code that I am trying to compile on the fly:

namespace A
{
    class Test
    {
        public static void Test()
        {                
            int.TryParse("12", out int result);
        }
    }
}

I usually use CSharpCodeProvideror CodeDomProvider. Unfortunately, when switching to C # 6 / C # 7, the following error occurs:

bug CS1644: the expression expression function cannot be used because it is not part of the C # 6.0 language specification

Of course, all of the code contains C # 7 functions, and they compile just fine using msbuild / xbuild.

I understand the error, but I do not know if there is another way to compile this code?

An important note - I run it in Mono, but I have no way to try .Net right now. This may be a problem with mono, but it seems to be common.

+4
1

, , , , , .

CodeDOM, Mono, Ubuntu 16.04 (Mono 4.2.1). , /langversion:experimental.

CodeDOM , CompilerParameters.CompilerOptions, :

compiler.CompileAssemblyFromSource(
    new CompilerParameters { CompilerOptions = "/langversion:experimental" }, code);

Mono 4.2.1. , .

, Mono .

, /langversion:experimental Mono, , , .Net.

+4

Source: https://habr.com/ru/post/1684730/


All Articles