Dawn.ValueString 2.1.0

Value String

ValueString allows you to encapsulate an object as a culture-invariant string and parse it to any type that implements the Parse/TryParse pattern.

Install-Package Dawn.ValueString -Version 2.1.0
dotnet add package Dawn.ValueString --version 2.1.0
<PackageReference Include="Dawn.ValueString" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dawn.ValueString --version 2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ValueString is intended to be used for convenience when there is a need to initialize typed instances from culture-neutral (invariant) strings - often read from a simple configuration file or a database table.

// ValueString uses the invariant culture when converting
// objects to string if they implement IFormattable.
// So the following value contains "1.5" (dot-separated) even if (1.5).ToString()
// returns "1,5" (comma-separated) due to the current culture.
var value = ValueString.Of(1.5);

// "As" method uses the invariant culture by default.
// It also has an overload accepting an IFormatProvider.
var number = value.As<double>(); // Calls double.Parse.

// Nullable values are supported.
value = ValueString.Of(default(double?));
number = value.As<double>(); // Throws an InvalidCastException.
var nullable = value.As<double?>(); // null.

// Enums are supported.
value = ValueString.Of("Red"); // Enum field name.
var red = value.As<ConsoleColor>();

value = ValueString.Of("12"); // Enum field value.
var green = value.As<ConsoleColor>();

// You can also cast a "dynamic" ValueString to the target type directly.
dynamic d = value;
number = (double)d;

// "Is" is just like "As", but calls the type's TryParse method instead of Parse.
value = ValueString.Of("1.1.1.1");
if (value.Is(out IPAddress address)) // Calls IPAddress.TryParse.
    Console.WriteLine("The IP address is: {0}", address);

// An implicit operator exists converting strings to ValueString instances.
value = "1.5";

See the full readme for the information regarding the other functions, internals, performance and special cases.

ValueString is intended to be used for convenience when there is a need to initialize typed instances from culture-neutral (invariant) strings - often read from a simple configuration file or a database table.

// ValueString uses the invariant culture when converting
// objects to string if they implement IFormattable.
// So the following value contains "1.5" (dot-separated) even if (1.5).ToString()
// returns "1,5" (comma-separated) due to the current culture.
var value = ValueString.Of(1.5);

// "As" method uses the invariant culture by default.
// It also has an overload accepting an IFormatProvider.
var number = value.As<double>(); // Calls double.Parse.

// Nullable values are supported.
value = ValueString.Of(default(double?));
number = value.As<double>(); // Throws an InvalidCastException.
var nullable = value.As<double?>(); // null.

// Enums are supported.
value = ValueString.Of("Red"); // Enum field name.
var red = value.As<ConsoleColor>();

value = ValueString.Of("12"); // Enum field value.
var green = value.As<ConsoleColor>();

// You can also cast a "dynamic" ValueString to the target type directly.
dynamic d = value;
number = (double)d;

// "Is" is just like "As", but calls the type's TryParse method instead of Parse.
value = ValueString.Of("1.1.1.1");
if (value.Is(out IPAddress address)) // Calls IPAddress.TryParse.
    Console.WriteLine("The IP address is: {0}", address);

// An implicit operator exists converting strings to ValueString instances.
value = "1.5";

See the full readme for the information regarding the other functions, internals, performance and special cases.

Release Notes

The assembly no longer has SecurityTransparentAttribute.

Version History

Version Downloads Last updated
2.1.0 301 3/15/2018
2.0.0 1,088 12/5/2017
1.5.0 179 12/4/2017
1.4.1 190 11/30/2017
1.4.0 193 11/22/2017
1.3.2 195 11/17/2017
1.3.1 216 11/15/2017
1.3.0 196 11/13/2017
1.2.2 205 11/12/2017
1.2.1 194 11/12/2017
1.2.0 196 11/9/2017
1.1.1 192 10/29/2017
1.1.0 165 10/27/2017
1.0.2 165 10/13/2017
1.0.1 254 12/5/2016
1.0.0 284 12/3/2016
0.1.0 477 11/29/2016