-
-
Notifications
You must be signed in to change notification settings - Fork 105
How Formatters Work
While source extensions translate complex types into objects, which can be represented as a string
, formatters determine the way these objects are output.
Formatters are added by calling
SmartFormatter.AddExtensions(...)
SmartFormatter.InsertExtension(...)
With AddExtensions(...)
all WellKnownExtensionTypes.Sources
and WellKnownExtensionTypes.Formatters
are automatically inserted to the extension list at the place where they usually should be.
InsertExtension(...)
lets you insert an extension to the desired position in the list.
// Add needed source extensions
var smart = new SmartFormatter()
// Add source extensions
.AddExtensions(new ReflectionSource(), new DefaultSource())
// Add formatter extensions
.AddExtensions(new ListFormatter(), new DefaultFormatter);
// Add all default source and formatter extensions
smart = Smart.CreateDefaultFormatter();
From a performance perspective, only register formatter extensions that are actually needed.
Different formatters may be able to format the same object type (e.g. null
). Some formatters have property CanAutoDetect = true
, meaning they can find out whether they can process the format. That's why the order in the list of registered formatters is important.
Example:
The ConditionalFormatter
can be invoked explicitly and with auto-detection:
// Include the name of the formatter
Smart.Format("{0:cond:{}|Empty}", "Hello");
// Rely on the CanAutoDetect feature
Smart.Format("{0:{}|Empty}", "Hello");
The recommendation, is to always include the (optional) formatter name in the format.
- 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