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:
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:
var formatString = Properties.Settings.Default.LogFormat;
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:
using CenterCLR;
var formatString = Properties.Settings.Default.LogFormat;
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:
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:
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:
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!