-
-
Notifications
You must be signed in to change notification settings - Fork 105
Common Pitfalls
Most of the examples in this Wiki are using "Smart.Format(...)". This is for a good reason: Smart.Format() automatically initializes the SmartFormatter with default extensions. If you're using SmartFormatter.Format() directly, then it's your job to initialize. So for the beginning just leave SmartFormatter.Format() alone.
Note: Smart.Format(...)
is just the short version for Smart.Default.Format(...)
.
By default, SmartFormat sets ErrorAction
for formatter and parser to ErrorAction.Ignore
. This can lead to confusing results. It's highly recommended, to turn exceptions on while developing and debugging the code:
Smart.Default.ErrorAction = ErrorAction.ThrowError;
Smart.Default.Parser.ErrorAction = ErrorAction.ThrowError;
Besides throwing exceptions it is possible, to trace any formatting or parsing errors by subscribing to the corresponding events. When using Smart.Format(...)
these events are:
var badPlaceholders = new HashSet<string>();
Smart.Default.OnFormattingFailure += (sender, args) => { badPlaceholders.Add(args.Placeholder); };
var parsingErrorText = new HashSet<string>();
Smart.Default.Parser.OnParsingFailure += (sender, args) => { parsingErrorText.Add(args.RawText); };
These events fire no matter how ErrorAction
of the formatter or parser are set. Opposed to exceptions, all errors will be reported, not only the first failure. Going this way you can decide in your code more fine grained, how to deal with errors.
Out of the box SmartFormat is a drop-in replacement for string.Format
. The consequence is, that curly braces are escaped the string.Format
way. So if the desired output shall be {literal}
, it means doubling the open and closing curly braces:
string.Format("{{literal}}")
Smart.Format("{{literal}}")
This string.Format
compatibility, however, causes problems when using SmartFormat's extended formatting capabilities, like
// This won't work with default settings!
Smart.Format("{0:{Persons:{Name}|, }}", model);
The reason is the double curly braces at the end of the format string, which the parser will escape, leading to a missing closing brace exception. Luckily, this easy to solve:
// This will work
Smart.Default.Parser.UseAlternativeEscapeChar('\\');
Smart.Format("{0:{Persons:{Name}|, }}", model);
With this setting the output {literal}
can be achieved by Smart.Format("\{literal\}")
.
When running on .NET Framework 4.x SmartFormat is using System.Runtime.Remoting.Messaging.CallContext.LogicalGet|SetData
.
With this in place, we know that dotnetfiddle.net will throw a security exception. When compiling SmartFormat for .Net Core, LogicalGet|SetData
is replaced by AsyncLocal<T>
, which does not bring this issue. Unfortunately AsyncLocal<T>
is supported only on .NET Framework version 4.6 or later.
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License