Dawn.ValueString 2.1.0

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.
#r "nuget: Dawn.ValueString, 2.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dawn.ValueString as a Cake Addin
#addin nuget:?package=Dawn.ValueString&version=2.1.0

// Install Dawn.ValueString as a Cake Tool
#tool nuget:?package=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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dawn.ValueString:

Package Downloads
OpenSoftware.OptionParser
OptionParser is a library that deals with option parsing and option handling for command-line (CLI) programs.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.1.0 1,370 3/15/2018
2.0.0 1,666 12/5/2017
1.5.0 536 12/4/2017
1.4.1 530 11/30/2017
1.4.0 516 11/22/2017
1.3.2 707 11/17/2017
1.3.1 728 11/15/2017
1.3.0 703 11/13/2017
1.2.2 736 11/12/2017
1.2.1 717 11/12/2017
1.2.0 720 11/9/2017
1.1.1 711 10/29/2017
1.1.0 510 10/27/2017
1.0.2 507 10/13/2017
1.0.1 610 12/5/2016
1.0.0 722 12/3/2016
0.1.0 1,001 11/29/2016