2

My code is this:

public static bool CompileClient(out string[] errors)
{
    using (CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"))
    {
        CompilerParameters cp = new CompilerParameters
        {
            GenerateExecutable = true,
            OutputAssembly = $"./hi.exe",
            CompilerOptions = "/optimize",
            TreatWarningsAsErrors = false
        };
        if (provider.Supports(GeneratorSupport.EntryPointMethod))
            cp.MainClass = "Namespace.Program";

        CompilerResults cr = provider.CompileAssemblyFromFile(cp, Directory.GetFiles("./Sourcecode/")); // Invoke compilation

        if (cr.Errors.Count > 0)
        {
            List<string> _errors = new List<string>();
            for (int i = 0; i < cr.Errors.Count; i++)
                _errors.Add(cr.Errors[i].ToString());
            errors = _errors.ToArray();
            return false;
        }
        else
        {
            errors = null;
            return true;
        }
    }
}

The .cs Files in /Sourcecode/ contain the following code: $"Hello {name}"

When I compile the Files I get this Error: error CS1056: Unexpected character '$'.

Is there a way to fix the error without using string + string or string.Format()? maybe changing the version or something..?

1 Answer 1

2

The $ notation for "string interpolations" (basically shorthand for string.Format()) was introduced in c# version 6, so you need a compiler that supports at least that version.

This answer might help:

Which C# compiler version to compile C# 7.3 with the CSharpCodeProvider class?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.