From Thomas Levesque, a bunch of usefull string extension and a string format extended to fields and properties (use Regex to find named placeholder and Expression compilation to access it).

Supported syntax is less rich (no sub-property, no indexer) than DataBinder solution (like FormatWith 2.0 – String formatting with named variables) but it doesn’t require dependency on System.Web and can be PCL).

thomaslevesque/NString · GitHub.

Edit: We can refer to Fun With Named Formats, String Parsing, and Edge Cases and .Net ObjectFormatter – Using Tokens in a Format String for two other implementations and interesting discussion of how to escape brace and how to proceed. Basically, we have:

  1. to find {Xxxx}, either Regex or a hand coded parser using a small state machine,
  2. to evaluate Xxxx against an object instance :
    1. pure Reflexion to read field or property,
    2. Expression.Compile to read field or property,
    3. DataBinder.Eval with a rich syntax but slow and introducing a dependency on System.Web,
  3. to replace {Xxxx}, either Regex.Replace or using a generated standard String.Format with positional {2} or a manual construction.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s