0

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. 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)

What mistake did I make?

Emilia Tyl
  • 565
  • 1
  • 5
  • 17

1 Answers1

3

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:

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.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • 1
    I get System.FormatException, not IndexOutOfRangeException. I wonder if the exception's being thrown somewhere else. At any rate I'd tell her about "{{" escaping. – 15ee8f99-57ff-4f92-890c-b56153 Sep 19 '16 at 18:36
  • 2
    @EdPlunkett: Given the stack trace, I suspect that may be a difference between the MS implementation and Mono. – Jon Skeet Sep 19 '16 at 18:40
  • @JonSkeet Good point, thanks -- I didn't notice that it was mono. – 15ee8f99-57ff-4f92-890c-b56153 Sep 19 '16 at 18:41
  • I don't think that escape chars are working, as the string is created with the preceding @. This means take it as it is. A Workaround could be to replace the opening curly braket at the end with {2} and add it as a third parameter '{' in the AppendFormat call. – Thomas Voß Sep 19 '16 at 18:43
  • @EdPlunkett Thanks for the escape hint! :D It works now :) – Emilia Tyl Sep 19 '16 at 18:44
  • @ThomasVoß '@' affects regular C# string literal escaping, which is done in the lexer or something. The curly brace here is being interpreted by the `Format` method at runtime. '{' has no special meaning to the compiler unless it's a '$' interpolation string -- which this isn't. – 15ee8f99-57ff-4f92-890c-b56153 Sep 19 '16 at 18:51