Format.NET is an easy to use, open source, library that enables advanced and smart object formatting in .NET projects. Extends the default String.Format(...) with property resolution, custom formatting and text alignment.
Features
Same behavior of String.Format(...) for simple object formatting
Format.ToString("{0} {1,-20} {2:dd/MM/yyyy}", person.Name, person.Surname, person.BirthDate);
---> "Riccardo Gregori 22/12/1983"
Property value resolution with dot notation
Format.ToString("{0.Name} {0.Surname,-20} {0.BirthDate:dd/MM/yyyy}", person);
---> "Riccardo Gregori 22/12/1983"
Property chaining
Format.ToString("{0.Name} is son of {0.Father.Name}", person);
---> "Riccardo is son of Marco"
Format.ToString("{0.Name} is grandson of {0.Father.Father.Name}", person);
---> "Riccardo is grandson of Antonio"
Default object inference, with multiple-objects support
Format.ToString("{Name} {Surname,-20} {BirthDate:dd/MM/yyyy}", person);
---> "Riccardo Gregori 22/12/1983"
Format.ToString("{Name} {Surname} born the {BirthDate:dd/MM/yyyy} in {1.Name}", person, city);
---> "Riccardo Gregori born the 22/12/1983 in Pescara"
Culture support
Format.ToString( new CultureInfo( "it-IT"), "{0:dddd}", new DateTime( 2010, 12, 4 ) );
---> "sabato"
Extension method support
person.ToSmartString("{Name} {Surname} born the {BirthDate:dd/MM/yyyy} in {1.Name}", city);
---> "Riccardo Gregori born the 22/12/1983 in Pescara"
Formatter factory with fluent configuration
var formatter = Formatter.CreateFormatter()
.UseFormatProvider( new CultureInfo( "it-IT") );
formatter.Format( "{0:dddd}", new DateTime( 2010, 12, 4 ) );
---> "sabato"
Default value for null properties support (only using fluent configuration)
var formatter = Formatter.CreateFormatter().DefaultIfNull( "[null] ");
formatter.Format( "{0}", null );
---> "[null]"
Default constants
Format.ToString("FirstLine{@NewLine}SecondLine");
---> "FirstLine
SecondLine"
Format.ToString("{@Today:dd/MM/yyyy}");
---> "04/12/2010"
Format.ToString("{@Now:dd/MM/yyyy HH:mm:ss}");
---> ";04/12/2010 13:38:17"
Wishlist...
Indexed properties
Format.ToString("{Children[0].Name}", person);
---> "Mario"
Iterators
var format = "{#each 0.Children as 1}{1.Name}, {#loop}"
Format.ToString(format, person);
---> "Mario, Giovanni, Antonio, "
Converters
var formatter = Formatter.CreateFormatter()
.Transform<bool, BoolToYesNoConverter>();
Format.ToString("{0}/{1}", true, false);
---> "yes/no";
About me...
My blog: http://community.visual-basic.it/greg
Follow me on twitter: http://twitter.com/_neronotte