[英]C# CodeDomCompiler. error CS1056: Unexpected character '$'
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}"
/Sourcecode/ 中的 .cs 文件包含以下代码: $"Hello {name}"
When I compile the Files I get this Error: error CS1056: Unexpected character '$'.
当我编译文件时,我收到此错误: error CS1056: Unexpected character '$'.
Is there a way to fix the error without using string + string or string.Format()?有没有办法在不使用 string + string 或 string.Format() 的情况下修复错误? maybe changing the version or something..?也许改变版本或什么..?
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. c# 版本 6 中引入了“字符串插值”的 $ 表示法(基本上是 string.Format() 的简写),因此您需要一个至少支持该版本的编译器。
This answer might help:这个答案可能会有所帮助:
Which C# compiler version to compile C# 7.3 with the CSharpCodeProvider class? 哪个C#编译器版本可以用CSharpCodeProvider class编译C# 7.3?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.