[英]StringBuilder AppendFormat throws IndexOutOfRangeException
I have a template define in a string: 我在字符串中有一个模板定义:
public static string EntityClassBegginingTemplate =
@"using System.Collections.Generic;
//generated by the RuleDesigner
public abstract class {0}Base : {1}
{";
And then I'm trying it to format a string: 然后我正在尝试格式化字符串:
builder.AppendFormat(Templates.EntityClassBegginingTemplate, entityName, baseClass);
That line throw an exception: 该行抛出异常:
IndexOutOfRangeException: Array index is out of range.
IndexOutOfRangeException:数组索引超出范围。 System.String.FormatHelper (System.Text.StringBuilder result, IFormatProvider provider, System.String format, System.Object[] args) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/String.cs:1912) System.Text.StringBuilder.AppendFormat (IFormatProvider provider, System.String format, System.Object[] args) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Text/StringBuilder.cs:534) System.Text.StringBuilder.AppendFormat (System.String format, System.Object arg0, System.Object arg1) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Text/StringBuilder.cs:555)
System.String.FormatHelper(System.Text.StringBuilder结果,IFormatProvider提供程序,System.String格式,System.Object [] args)(位于/ Users / builduser / buildslave / mono / build / mcs / class / corlib / System / String .cs:1912)System.Text.StringBuilder.AppendFormat(IFormatProvider提供程序,System.String格式,System.Object []参数)(位于/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Text /StringBuilder.cs:534)System.Text.StringBuilder.AppendFormat(System.String格式,System.Object arg0,System.Object arg1)(位于/ Users / builduser / buildslave / mono / build / mcs / class / corlib / System .Text / StringBuilder.cs:555)
What mistake did I make? 我犯了什么错误?
I would assume that the opening curly brace for the class template is being interpreted as a placeholder. 我假设类模板的开头大括号被解释为占位符。 You'll need to escape any curly braces that you want treated as literal characters.
您需要转义要当作文字字符的所有花括号。
public static string EntityClassBegginingTemplate =
@"using System.Collections.Generic;
//generated by the RuleDesigner
public abstract class {0}Base : {1}
{"; <-- this is where the issue likely originated
As Ed Plunkett notes, you escape braces by using the double-brace notation , {{
, as covered in the MSDN : 如Ed Plunkett所述,您可以使用MSDN中介绍的双大括号符号
{{
来转义大括号:
Opening and closing braces are interpreted as starting and ending a format item.
左括号和右括号被解释为格式项的开始和结束。 Consequently, you must use an escape sequence to display a literal opening brace or closing brace.
因此,必须使用转义序列来显示文字的右括号或右括号。 Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}").
在固定文本中指定两个开括号(“ {{”)以显示一个开括号(“ {”),或两个闭括号(“}}”)以显示一个闭括号(“}”)。 Braces in a format item are interpreted sequentially in the order they are encountered.
格式项中的大括号将按其遇到的顺序顺序进行解释。 Interpreting nested braces is not supported.
不支持解释嵌套括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.