简体   繁体   English

C# CodeDomCompiler。 错误 CS1056:意外字符“$”

[英]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.

相关问题 C#错误CS1056:Jenkins或MSbuild出现意外字符&#39;$&#39; - c# error CS1056: Unexpected character '$' with Jenkins or MSbuild 错误CS1056:意外字符&#39;$&#39;Xamarin Studio - Error CS1056: Unexpected character '$' Xamarin Studio C#类错误CS1061 CS1056 - C# class error CS1061 CS1056 为什么我在此代码上得到一个 CS1056 - Unexpected character '' - Why do I get an CS1056 - Unexpected character '' on this code 为什么我会收到“错误 CS1056:意外字符 &#39;$&#39;”并在此处放置 $ 标记? 我的编译器有问题吗? - Why am I getting "error CS1056: Unexpected character '$'" putting a $ mark here? A problem with my compiler? 错误 CS1056:在 tfs 持续集成过程中运行 msbuild 时出现意外字符“$” - Error CS1056: Unexpected character '$' running the msbuild on a tfs continuous integration process 为什么我在此代码中得到 CS1056 意外字符 '' - Why do I get an CS1056 Unexpected character '' on this code 如何调试错误“意外的字符CS1056”? - How to debug the error “unexpected char CS1056”? 如何修复 C# 错误 CS1525:意外符号“if”“sol”和“else”? [等候接听] - How to fix C# error CS1525: Unexpected Symbol 'if' 'sol' and 'else'? [on hold] C# 错误 CS1503 参数 1:无法从“字符串”转换为“字符” - C# Error CS1503 Argument 1: cannot convert from 'string' to 'Character'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM