deniszykov.TypeConversion
3.0.1
See the version list below for details.
dotnet add package deniszykov.TypeConversion --version 3.0.1
NuGet\Install-Package deniszykov.TypeConversion -Version 3.0.1
<PackageReference Include="deniszykov.TypeConversion" Version="3.0.1" />
paket add deniszykov.TypeConversion --version 3.0.1
#r "nuget: deniszykov.TypeConversion, 3.0.1"
// Install deniszykov.TypeConversion as a Cake Addin #addin nuget:?package=deniszykov.TypeConversion&version=3.0.1 // Install deniszykov.TypeConversion as a Cake Tool #tool nuget:?package=deniszykov.TypeConversion&version=3.0.1
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
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 is compatible. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.6 is compatible. netstandard2.0 was computed. netstandard2.1 is compatible. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.1
- Microsoft.Extensions.Options (>= 3.1.10)
-
.NETCoreApp 3.0
- Microsoft.Extensions.Options (>= 3.1.10)
-
.NETFramework 4.5
- No dependencies.
-
.NETFramework 4.6.1
- Microsoft.Extensions.Options (>= 5.0.0)
-
.NETStandard 1.6
- Microsoft.Extensions.Options (>= 1.1.2)
- NETStandard.Library (>= 1.6.1)
- System.Runtime.Serialization.Primitives (>= 4.3.0)
-
.NETStandard 2.1
- Microsoft.Extensions.Options (>= 3.1.10)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on deniszykov.TypeConversion:
Package | Downloads |
---|---|
Pluto
Pluto is an application server |
|
deniszykov.CommandLine
Command line parser and binder. Provides API for parsing and binding command line arguments to .NET methods. |
|
RoguelikeToolkit.Entities
Package Description |
|
WebView2.DOM
C# DOM bindings to be used with WebView2 |
|
SliccDB
A simple, one file graph database! |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on deniszykov.TypeConversion:
Repository | Stars |
---|---|
pmikstacki/SliccDB
Light Embedded Graph Database for .net
|
# 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.