Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / string

NamingFormatter - String Interpolation in Runtime Solution

4.20/5 (5 votes)
10 Feb 2016Apache3 min read 13.8K   58  
This library is simple and is a substitution of System.String.Format.

License is Apache V2. I made Profile1 / Profile259 in PCL, NET2.0 and NET3.5 correspond, so it's practicable by most environments.

Introduction

NamingFormatter is string interpolation, easy to use multi target framework library for .NET.

Standard System.String.Format uses "index number" for checking with arguments. The NamingFormatter can be used with key-string based arguments.

Background

An example of System.String.Format where the below is ordinary:

C#
var formatted = string.Format(
    "Index0:{0}, Index1:{1}",
    arg0,
    arg1);

The index number in the format string is just the numerical value, and a problem of this cord is that there are no arg0 on the cord, arg1 and direct relation. "String Interpolation" in C# 6, but this is the evaluation when compiling time.

For example, I think there is a situation in which the user is able to designate a format string as which optionally. Format designation of logger output is a good example, but when I make them designate it by the index number, what each number indicates becomes incomprehensible.

An example of logger output is indicated:

C#
// ex: Format string become from App.config.
//   "Date: {2:yyyyMMdd}, UserName: {0} and Action: {1}"
var formatString = Properties.Settings.Default.LogFormat;

// Format now, how to matching arguments?
var formatted = string.Format(
    formatString,
    userName,
    action,
    date);

A format string is chosen as external input as mentioned above, and, it's possible to customize, when doing, what that number of the index number says in many ways becomes very incomprehensible? When NamingFormatter is used in such case, it's possible to designate a format string as follows:

C#
using CenterCLR;

// ex: Format string become from App.config.
//   "Date: {date:yyyyMMdd}, UserName: {userName}, Action: {action}"
var formatString = Properties.Settings.Default.LogFormat;

// Format now, use Named.Format method.
var formatted = Named.Format(
    formatString,
    Named.Pair("userName", userName),
    Named.Pair("action", action),
    Named.Pair("date", date));

The Named.Pair method is the utility method to generate KeyValuePair<string, object>. Of course, use "operator new", no problem.

It includes the following as overloads:

  • params KeyValuePair<string and object>[]: It's the method corresponding to the variable argument used by an example mentioned above.
  • IEnumerable<KeyValuePair<string, object>>: When handing a result of LINQ and making them do the format, it's possible to designate IEqualityComparer and customize specification method of keys.
  • Dictionary<string, object>: When existing as a dictionary already, it's possible to use this. Another option can use IDictionary and IReadOnlyDictionary interfaces.
  • There is also a method as which Func<string, object> is designated as the most basic overload. When this method is used, the value which corresponds to key name can be customized perfectly.
  • There is also an overload which can use IFormatProvider interface for the format.

Format Options

Format options are the function where it's possible to make how to form values additionally. An example mentioned above was indicated:

C#
// Which does form designation of a date (DateTime structure)
var formatted = Named.Format(
    "Date: {date:yyyy/MM/dd HH:mm:ss.fff}",
    Named.Pair("date", date));

It's possible to designate an option like the form designation designated in System.String.Format.

Or:

C#
// Which designates the number of figures of a figure
var formatted = Named.Format(
    "Result: {result,10}",
    Named.Pair("result", 123));

It's possible to do the number of figures designation. Of course, it's possible to combine these and make them designate more than arguments at the same time.

The Traverse Function of Properties

Which correspond to a parameter aren't the primitive type, it's possible to make them search for public properties into another class types or structure types. This is "a dot-notation expression" like the binding system of XAML:

C#
// Which makes a property of DateTime search by a format string
var formatted = Named.Format(
    "Millisec: {date.TimeOfDay.TotalMilliseconds}",
    Named.Pair("date", DateTime.Now));

As the condition, in public and instanced properties. Example code was used taking the DateTime structure for instance here, but it's possible to use any of your-defined classes and structures of course. When it fails to traverse properties, return empty string (such as the property name is wrong.)

Conclusion

It's small in a case with an environment-customization-important matter in particular, I think they may be able to apply. The license has been also made loose (Apache V2), so please try it out. Thanks!

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0