TypeConvert 2.1.6

.NET Core 2.0 .NET Standard 1.3 .NET Framework 3.5
Suggested Alternatives

deniszykov.TypeConversion

dotnet add package TypeConvert --version 2.1.6
NuGet\Install-Package TypeConvert -Version 2.1.6
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="TypeConvert" Version="2.1.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TypeConvert --version 2.1.6
#r "nuget: TypeConvert, 2.1.6"
#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 TypeConvert as a Cake Addin
#addin nuget:?package=TypeConvert&version=2.1.6

// Install TypeConvert as a Cake Tool
#tool nuget:?package=TypeConvert&version=2.1.6

Build Status

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).

TypeConvert combines all these approaches under one API. Additionally this package has hexadecimal conversion API and type instancing API(cached for better perfomance). Each utility class in this package can be used separately and you are free to embedd them into your project.

TypeConvert
// generic
ToType Convert<FromType, ToType>(value, [format], [formatProvider]);
bool TryConvert<FromType, ToType>(value, out result, [format], [formatProvider])
string ToString<FromType>(value, [format], [formatProvider]);
// non-generic
object Convert(value, toType, [format], [formatProvider]);
bool TryConvert(ref value, toType, [format], [formatProvider]);
string ToString(value, [format], [formatProvider]);
TypeActivator
object CreateInstance(type, forceCreate = false);
object CreateInstance<Arg1T, ...>(type, arg1 ...);
HexConvert
string ToString(buffer, offset, count); // bytes -> hex
byte[] ToBytes(hexString, offset, count); // hex -> bytes

void Encode(buffer, offset, count, hexBuffer, hexBufferOffset); // bytes chunk -> hex chunk
void Decode(hexBuffer, offset, count, buffer, bufferOffset); // hex chunk -> bytes chunk

int WriteTo(number, hexBuffer, offset); // number -> hex
Number ToNumber(hexBuffer, offset) // hex -> number

Installation

Install-Package TypeConvert 

Examples

TypeConvert

Convert from string to integer

TypeConvert.Convert<string, int>("1") // 1

Convert from integer to Enum

TypeConvert.Convert<int, ConsoleColor>(1) // DarkBlue

Convert from string to IPAddress

TypeConvert.Convert<string, IPAddress>("127.0.0.1") // 127.0.0.1 via IPAddress.Parse

Convert to string with format(if supported)

TypeConvert.ToString(1000000, format: "x") // f4240

Convert from any to IpAddress

TypeConvert.Convert("127.0.0.1", typeof(IPAddress)); 127.0.0.1 via IPAddress.Parse

Testing conversion

TypeConvert.TryConvert<string, int>("xxx", out intValue) // false

TypeActivator

Creating from default constructor

TypeActivator.CreateInstance(typeof(int)); // 0

Getting class instance without default constructor

TypeActivator.CreateInstance(typeof(EventArgs)); // via EventArgs.Empty

Getting class instance without default constructor and Empty property

TypeActivator.CreateInstance(typeof(IPEndPoint), force: true); // IPEndPoint bypassing constructor

Creating an array

TypeActivator.CreateInstance(typeof(int[])); // int[0] (same instance every time)
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net35 net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.0

    • No dependencies.
  • .NETCoreApp 2.1

    • No dependencies.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 1.3

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.6 2,532 11/11/2020
2.1.5 565 9/26/2020
2.1.4 4,186 7/5/2018
2.1.3 769 6/20/2018
2.1.2 901 5/31/2018
2.1.0 866 5/29/2018
2.0.0 1,060 5/21/2018
1.2.0 1,067 1/14/2018

# 2.1.6
- fixed 'by-ref-like' parameter errors from .NET 5.0

# 2.1.5
- added case-insensitive EnumHelper.Parse overloads
- fixed: Uri -> string conversion should use Uri.Original string instead of Uri.ToString()
- fixed: HexConvert.ToString(buffer, offset, count) dont respect count parameter
- fixed: HexConvert.ToBytes(hexBuffer, offset, count) dont respect count parameter
- fixed: `Unable to cast object of type 'System.Linq.EmptyPartition1[System.TypeConvert+ConvertMethodInfo]' to type 'ConvertMethodInfo[]'` due breaking change in .NET 3.0


# 2.1.4
- added TypeConvert.RegisterCustomConversion method that allow override conversion operation between selected types.
- fixed Uri conversion. Relative url is now supported.

# 2.1.3
- fixed conversion methods (ToXXX, FromXXX) on value types cause delegate binding exception

# 2.1.2
- added netstandard2.0 and netcoreapp2.1 target platform
- fixed error with netcoreapp2.1 ByRefLike types (they are not supported in conversion operations)
- fixed error with string->enum conversion (new tests are added)

# 2.1.0
- added conversion via constructor support
- added System.ComponentModel.TypeConverter support for netstandard2.0 platform


# 2.0.0
- added fast conversion for numbers (byte, int, float...)
- added priority of conversion methods (formattable methods, implicit operators, explicit operators, Parse/Create/From/To methods, System.Convert methods)
- optimized performance by cutting redundant initialization of internal classes
- added documentation

# breaking changes
- non-generic Convert and TryConvert signature has been changed. Parameter "fromType" has been removed. Order of parameters has been changed.
- new priority of conversion methods could lead to conversion result that differs from v1.2.0.
- TypeConvert.ToString now returns String.Empty instead of null when null value is passed.
- changed signature of HexConvert's methods for better semantic

# 1.2.0
- fixed error in type activator parameters binding (now more specific types binds successfully)
- added new EnumHelper enum's properties DefaultValue, MinValue, MaxValue, Names, Values, UnderlyingType
- added forth argument to TypeActivator.CreateInstance methods
- TypeActivator signature fix to match .NET 4.5 and .NET Standard 1.3/2.0 build targets

# 1.1.0
- added EnumHelper for enum conversion between number and object form
- added '.NET Standard 1.3' platform target

# 1.0.0.1
- fixed object <-> nullable value, enum <-> nullable conversions