1

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..?

Void
  • 45
  • 9

1 Answers1

1

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?

Christopher Hamkins
  • 1,442
  • 9
  • 18