My .net 4.8 code compiles properly and _ = "test";
doesn't throw an error when using this in visual studio. However when using CsharpCodeProvider this doesn't compile. (this can be easily verified by commenting out this line in the string. How can I tell the CodeProvider to use .net framework 4.8? I know there is a flag that can be set using compiler options but this throws an error about missing csc.exe when using v4.8.
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
_ = "test";
var code = @"
public class Abc {
public string Get() { return ""abc""; }
public string Test(){
_ = ""test"";
return ""test"";
}
}
";
var options = new CompilerParameters() { };
options.GenerateExecutable = false;
options.GenerateInMemory = false;
var provider = new CSharpCodeProvider() { };
var compile = provider.CompileAssemblyFromSource(options, code);
var type = compile.CompiledAssembly.GetType("Abc");
var abc = Activator.CreateInstance(type);
var method = type.GetMethod("Test");
var result = method.Invoke(abc, null);
Console.WriteLine(result); //output: abc
Console.Read();
}
}
}