![]() |
VOOZH | about |
dotnet add package UniversalTypeConverter --version 2.7.1
NuGet\Install-Package UniversalTypeConverter -Version 2.7.1
<PackageReference Include="UniversalTypeConverter" Version="2.7.1" />
<PackageVersion Include="UniversalTypeConverter" Version="2.7.1" />Directory.Packages.props
<PackageReference Include="UniversalTypeConverter" />Project file
paket add UniversalTypeConverter --version 2.7.1
#r "nuget: UniversalTypeConverter, 2.7.1"
#:package UniversalTypeConverter@2.7.1
#addin nuget:?package=UniversalTypeConverter&version=2.7.1Install as a Cake Addin
#tool nuget:?package=UniversalTypeConverter&version=2.7.1Install as a Cake Tool
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".
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>();
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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. |
|
|
Blazm.Components
A collection of Blazor components |
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 2.7.1 supports conversion from Guid to byte[]