Moelbeck
Moelbeck

Reputation: 640

CSharpCodeProvider use C# language version 7.1 or greater

I have a small console project where I'm trying to compile some C# files into a .dll.

The code looks like:

    public Result CreateDll(string[] files, string assemblies, string toPath, string dllName)
    {
        if (!Directory.Exists(toPath))
            Directory.CreateDirectory(toPath);

        using (var provider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider(new CompilerSettings()))
        {
            var parameters = new CompilerParameters(assemblies)
            {
                GenerateInMemory = false,
                GenerateExecutable = false,
                OutputAssembly = $@"{toPath}\{dllName}.dll",
                IncludeDebugInformation = false,
                TreatWarningsAsErrors = true,
                CompilerOptions = "/unsafe /optimize"
            };

            CompilerResults results = provider.CompileAssemblyFromFile(parameters, files);
        }

        return Result.Success;
    }

One of the files I'm trying to compile looks like:

    public abstract class BaseClass
{
    private string backendString;
    private string property;
    public string TestString => backendString;

    public string Property
    {
        get => property;
        set
        {
            if (value == default)
                return;
            property = value;
        }
    }
}

But I get this error:

Feature 'default literal' is not available in C# 7.0. Please use language version 7.1 or greater.

If I remove the *default* from BaseClass, then it does not complain. How do I change this?

Upvotes: 1

Views: 721

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

CompilerOptions = "/unsafe /optimize /langversion:7.1"

Upvotes: 2

Related Questions