C# utilities for convertion between types, type construction and inspection
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
TypeConvert.Tests
TypeConvert
.gitignore
.travis.yml
LICENSE
README.md
TypeConvert.sln

README.md

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)