我有一个在字符串中定义的模板:
public static string EntityClassBegginingTemplate =
@"using System.Collections.Generic;
//generated by the RuleDesigner
public abstract class {0}Base : {1}
{";然后我试着格式化一个字符串:
builder.AppendFormat(Templates.EntityClassBegginingTemplate, entityName, baseClass);这一行抛出一个异常:
IndexOutOfRangeException:数组索引超出了范围。( /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/String.cs:1912) System.Text.StringBuilder.AppendFormat (IFormatProvider提供者,System.String格式,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,(在/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Text/StringBuilder.cs:555) )( System.Object arg1)
我犯了什么错误?
发布于 2016-09-19 18:33:40
我假设类模板的开头大括号被解释为占位符。您将需要转义任何您希望作为文字字符处理的大括号。
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正如Ed所指出的,您可以使用双大括号表示法{{、如MSDN所述来转义大括号。
开始和结束大括号被解释为开始和结束一个格式项目。因此,您必须使用转义序列来显示文字开头大括号或结束大括号。在固定文本中指定两个开口大括号("{{"),以显示一个开口大括号("{")或两个结束大括号("}")以显示一个结束大括号(“}”)。格式项中的大括号按遇到的顺序依次解释。不支持解释嵌套大括号。
https://stackoverflow.com/questions/39579832
复制相似问题