C# 如何获取不分析{0}的String.Format

C# 如何获取不分析{0}的String.Format,c#,code-generation,string.format,C#,Code Generation,String.format,我正在编写一个代码生成工具,它经常会有这样的行 StringBuilder sp = new Stringbuilder(); sp.AppendFormat(" public {0}TextColumn()\n", className); sp.AppendLine(" {" sp.AppendLine(" Column = new DataGridViewTextBoxColumn();"); sp.AppendFormat("

我正在编写一个代码生成工具,它经常会有这样的行

StringBuilder sp = new Stringbuilder();
sp.AppendFormat("        public {0}TextColumn()\n", className);
sp.AppendLine("        {"
sp.AppendLine("            Column = new DataGridViewTextBoxColumn();");
sp.AppendFormat("            Column.DataPropertyName = \"{0}\";\n", columnName);
然而,我遇到的问题是,当我遇到这样一行时

sp.AppendFormat("return String.Format(\"{0} = '{0}'\", cmbList.SelectedValue);", columnName);
我希望第一个
{0}
转换为columnName的任何值,但我希望第二个
{0}
保持独立,以便内部
字符串.Format
将正确处理它


怎么做?

使用双大括号:

string result = string.Format("{{Ignored}} {{123}} {0}", 543);

可以使用两个大括号字符对大括号进行转义。各国的文件:

要在格式中指定单个文字大括号字符,请指定两个前大括号或后大括号字符;即“{{”或“}}”

在你的例子中,那将是

sp.AppendFormat("return String.Format(\"{0} = '{{0}}'\",cmbList.SelectedValue);", 
    columnName);

使用“{0}}”。这将产生字符串“{0}”

,顺便问一下,您是否知道Visual Studio中包含用于代码生成的T4引擎?@0xA3不知道,它是否与rt一起工作。单击“运行自定义工具”?这就是我写代码生成的目的。这些是我所指的定制工具。
sp.AppendFormat("return String.Format(\"{0} = '{{0}}'\", cmbList.SelectedValue);", columnName);
String.Format(\"{0} = '{{0}}'\",