deniszykov.TypeConversion 3.0.2

Install-Package deniszykov.TypeConversion -Version 3.0.2
dotnet add package deniszykov.TypeConversion --version 3.0.2
<PackageReference Include="deniszykov.TypeConversion" Version="3.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add deniszykov.TypeConversion --version 3.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: deniszykov.TypeConversion, 3.0.2"
#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 deniszykov.TypeConversion as a Cake Addin
#addin nuget:?package=deniszykov.TypeConversion&version=3.0.2

// Install deniszykov.TypeConversion as a Cake Tool
#tool nuget:?package=deniszykov.TypeConversion&version=3.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

dotnet_build

Introduction

For historical reasons, .NET has several approaches to value conversion:

  • Explicit/Implicit operators
  • System.Convert class
  • IConvertible interface
  • System.ComponentModel.TypeConverter
  • To, From, Parse, Create methods
  • Constructors (Uri, Guid)
  • Meta types (Enums, Nullable Types).

This package combines all these approaches under one API.

Installation

Install-Package deniszykov.TypeConversion 

Usage

TypeConversionProvider methods
// generic
ToType Convert<FromType, ToType>(fromValue, [format], [formatProvider]);
bool TryConvert<FromType, ToType>(fromValue, out result, [format], [formatProvider]);
string ConvertToString<FromType>(fromValue, [format], [formatProvider]);

// non-generic
object Convert(fromValue, toType, fromValue, [format], [formatProvider]);
bool TryConvert(fromValue, toType, fromValue, out result, [format], [formatProvider]);

Example

using deniszykov.TypeConversion;

  var conversionProvider = new TypeConversionProvider();
  var timeSpanString = "00:00:01";
  
  var timeSpan = conversionProvider.Convert<string, TimeSpan>(timeSpanString);
  
  // with default settings TimeSpan.Parse(value, format, formatProvider) 
  // is used for conversion inside Convert<string, TimeSpan>()

Configuration

using deniszykov.TypeConversion;

var configuration = new TypeConversionProviderConfiguration
{
  Options = ConversionOptions.UseDefaultFormatIfNotSpecified
};

#if NET45
var typeConversionProvider = new TypeConversionProvider(configuration);
#else
var typeConversionProvider = new TypeConversionProvider(Options.Create(configuration));
#endif

Or configure via DI

using deniszykov.TypeConversion;
using Microsoft.Extensions.DependencyInjection;

Host.CreateDefaultBuilder().ConfigureServices(IServiceCollection services) => {

  // add configuration
  services.Configure<TypeConversionProviderConfiguration>(options =>
  {
    options.DefaultFormatProvider = CultureInfo.CurrentUICulture;
  });

  // register service
  services.AddSingleton<ITypeConversionProvider, TypeConversionProvider>();
}

Providing custom conversion between types

using deniszykov.TypeConversion;
using Microsoft.Extensions.DependencyInjection;

Host.CreateDefaultBuilder().ConfigureServices(IServiceCollection services) => {

  services.Configure<TypeConversionProviderConfiguration>(options =>
  {
    // register custom conversion from Uri to string
    options.RegisterConversion<Uri, string>((uri, format, formatProvider) => uri.OriginalString);
  });
  
}

Preparing for AOT runtime

using deniszykov.TypeConversion;
using Microsoft.Extensions.DependencyInjection;

Host.CreateDefaultBuilder().ConfigureServices(IServiceCollection services) => {

  services.Configure<TypeConversionProviderConfiguration>(options =>
  {
    // disable optimizations which use dynamic code generation
    options.Options &= ~(ConversionOptions.OptimizeWithExpressions | ConversionOptions.OptimizeWithGenerics);
  });
  
}

Key Abstractions

interface ITypeConversionProvider; // provides methods to get IConverter
interface IConverter<FromType, ToType>; // converts values
interface IConversionMetadataProvider; // provides metadata for ITypeConversionProvider

NuGet packages (2)

Showing the top 2 NuGet packages that depend on deniszykov.TypeConversion:

Package Downloads
WebView2.DOM

C# DOM bindings to be used with WebView2

deniszykov.CommandLine

Command line parser and binder. Provides API for parsing and binding command line arguments to .NET methods.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.2 919 8/4/2021
3.0.1 948 2/26/2021
3.0.0 110 1/29/2021

# 3.0.2

fix: fixed usage of dynamic methods in EnumConversionInfo (frag was checked wrongly)
feature: added compatibility shim for old 'TypeConvert' package

# 3.0.0
- renamed project to `deniszykov.TypeConversion`
- removed HexConvert, Base64Convert, TypeActivator
- refactored TypeConvert to `ITypeConversionProvider` abstraction and `TypeConversionProvider` implementation
- added configurable behaviour via `TypeConversionProviderConfiguration` and `IConversionMetadataProvider`
- renamed `EnumHelper` to EnumConversionInfo and made it instantiable class instead of static class.