UniversalTypeConverter 2.7.1

dotnet add package UniversalTypeConverter --version 2.7.1                
NuGet\Install-Package UniversalTypeConverter -Version 2.7.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UniversalTypeConverter" Version="2.7.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UniversalTypeConverter --version 2.7.1                
#r "nuget: UniversalTypeConverter, 2.7.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UniversalTypeConverter as a Cake Addin
#addin nuget:?package=UniversalTypeConverter&version=2.7.1

// Install UniversalTypeConverter as a Cake Tool
#tool nuget:?package=UniversalTypeConverter&version=2.7.1                

UniversalTypeConverter

The Swiss Army Knife for type conversion in .NET

Release NuGet Version Static Badge NuGet Downloads

Convert any type to another compatible type without worrying about which method to use.

Just write

var result = myObj.To<T>()

to convert myObj to T

All imaginable types of conversions are integrated and can be configured in a variety of ways. You can even add your own conversion methods if something doesn't meet your needs. You can also use any number of converters simultaneously and configure them independently, e.g. to treat values from the database differently from values received via a Web API.

So you have the following extension methods to your hand:

To<T>():
Converts the given value to the given type using the default converter. Throws an InvalidConversionException if the value is not convertible to the given type.

IsConvertibleTo<T>():
Determines whether the given value can be converted to the specified type using the default converter. Returns true if the given value can be converted to the specified type; otherwise, false without throwing an exception.

As<T>():
Converts the given value to the given type using the default converter. The result provides access to the converted value if conversion succeeded and simplifies access to default values if conversion failed, e.g.:

var x = y.As<int>().OrDefault();	// returns 0 (the default of int) if y is not convertible to int.

ToEnumerable<T>():
Returns an iterator over the results of converting the given values to the given type.

Most extension methods provide an overload with the destination type as an parameter if using the generic version is not possible.

You are able to configure the default converter by setting the properties of UniversalTypeConverter.Options.

If you need another converter beside the default one, just create a new instance of "TypeConverter".

Mapping

Beside the obvious conversions, there are the following extension methods:

ToDictionary():
Creates a new dictionary whose key value pairs represent all non static public values of the given source.

The other way around you can create the instance of any type from an IDictionary<string, object> by calling:

Create<T>():
Creates a new instance of the given type by mapping the key value pairs of the given dictionary to constructor parameters, public properties and public fields of the given type. For success, at least on key value pair must be used as a constructor parameter, public property or public field. If the dictionary contains only one key value pair, its value is returned if it is exactly of the demanded type. Conversion is done automatically if a property does not fit to the destination type.

By using these two methods you are able to create any object from any other as long as there are some naming matches! Just use:

var x = y.ToDictionary().Create<X>();

ADO.NET

ToDictionary() handles IDataRecord (mostly a DataReader), DataRow and DataRowView slightly different. For these types the key value pairs of the created dictionary represent the fields. In addition to that, calling To<T>() on any of these types will use ToDictionary() and Create<T>() behind the scene - so the following will work:

var x = myDataRow.To<X>();

ToDataTable():
Works on IEnumerable<T> and creates a DataTable. Each element of the given source results in a row representing the properties of <T> as columns.


Notice
This is a complete rewrite of version 1 of the UniversalTypeConverter. If you are interested in why I started this project and what the basic thoughts and concepts were, I recommend you to read my article on codeproject.com, as most of the conversion methods apply to this version as well.

If you have already used the first version of the UniversalTypeConverter:
Some of the old extension methods are marked as "obsolete", but still work. I recommend replacing them with the new signatures, as the old variants will be removed at some point. However, there are also some methods and options that have been removed. If you have used them, you can expect Breaking Changes. If you are unsure: stay at version 1 on existing projects and use version 2 for new projects.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (17)

Showing the top 5 NuGet packages that depend on UniversalTypeConverter:

Package Downloads
CoreMore

Package Description

UiComm.Client

This package resolves the dependencies when creating a UiComm Client project.

Wyam.Core

Wyam is a simple to use, highly modular, and extremely configurable static content generator. This is the core engine and can be used to embed Wyam into your application. If you just want to create sites, download the Wyam executable from GitHub the Wyam website.

UiComm.Addin

This package resolves the dependencies when creating a UiComm Client project.

Node.Cs.Lib

Basic classes and implementations for Node.Cs. The documentation can be found on http://www.kendar.org/?p=/dotnet/nodecs.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on UniversalTypeConverter:

Repository Stars
zhupingqi/RuiJi.Net
crawler framework, distributed crawler extractor
Wyamio/Wyam
A modular static content and static site generator.
Version Downloads Last updated
2.7.1 2,637 7/25/2024
2.6.2 1,883 7/2/2024
2.6.0 90,177 6/21/2022
2.0.0 255,402 4/24/2019
1.0.5 74,710 5/2/2018
1.0.4 68,353 4/23/2017
1.0.2 97,709 7/7/2012
1.0.0 2,105 9/24/2011

Version 2.7.1 supports conversion from Guid to byte[]