Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#
Tip/Trick

Transformations Type Converter Library

Rate me:
Please Sign up or sign in to vote.
4.59/5 (8 votes)
26 May 2017CPOL2 min read 22.3K   12   3
NuGet package

Introduction

The transformations library is intended to cover type conversion, enumeration conversion and some additional helper methods.

Background

Type conversion is something that you inevitably end up having to do in nearly every walk of project/code life. A lot of times, they are hidden off in the dark murky depths of the code, but it always feels like there should be a better way of dealing with them.

Originally to master these evil demons, I have set out all the conversions in a separate static class which really seemed to fit the bill. Then on my quest to brighter conversion future, I stumbled across a Universal Type Converter library:

I really liked the concept and the approach and I think the author did a really good job.

There were a couple of things that bugged me though.

One of them is explained in the article, to quote its author:

“Just to give you an idea about what I was looking for:”

C#
int myInt = myGivenValue.ConvertTo<int>();

“Regardless of myGivenValue's type.”

However if you read on, the solution that was adopted was a bit different:

C#
int myInt = UniversalTypeConverter.ConvertTo<int>(myGivenValue);

Looking at the source code, it becomes apparent that the reason for that is because myGivenValue is an object type.

It’s meant to be either be a simple object or implement IConvertible for a custom user object. It’s true, the flexibility is very good, but the second seemed a bit of an overkill to me. Sticking with the simple types, I could confirm at compile time that the conversion actually works, which seemed like a greater benefit.

My other thought was that most of the time you would have a pretty clear idea about the default you would use if the conversion failed. You may wish to know that the conversion failed, but that does not mean you want an exception to actually happen, a default is more desirable and graceful.

NuGet Package

As a conclusion to the above, I have put together a little library which is shared on NuGet:

You can find the Transformations library via the NuGet Package Manager in Visual Studio.

Using the Code

There is no extensive help available for this project at this point.

Some examples of use:

C#
string valueInput = "7F8C14B6-B3A8-4F71-8EFC-E5A7B35923B6"; 
Guid actual = valueInput.ConvertTo<Guid>(Guid.Empty); 
// ----------------------------------------------- 
string valueInput = "0.1"; 
float actual = valueInput.ConvertTo<float>(0.0f); 
// result = 0.1f; 
// ----------------------------------------------- 
string valueInput = "15/02/2014"; 
DateTime actual = valueInput.ConvertTo<DateTime>(new DateTime(2000, 01, 01)); 
// result = new DateTime(2014, 02, 15); 
// ----------------------------------------------- 
float? f = 123.524f; 
int? i = f.ConvertTo<int>(); 
// result = 124; 
// -----------------------------------------------   

Obviously, you can't just expect every possible conversion to just work (think of casting an invalid string to an int for example). If the conversion fails, it assumes the default value for that type - for int, it would be 0. But what you want to absolutely know is "has the conversion actually worked or not"? Well, you also have TryConvertTo<T> at your disposal:

C#
// ----------------------------------------------- 
string s = "123";
int i;
if (s.TryConvertTo<int>(out i)) { /*do something here! */ } 
// result = 123; 
// -----------------------------------------------   

History

The beta version 0.0.6 is now available.

I am looking for some feedback from the community, the good the bad and the ugly, please.

The project is subject to change, please do not use this in production code as certain aspects may be updated. I am looking to re-write this library, so the final code is likely to look a bit different.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat about some performance values? Pin
Carlos19075-May-14 1:17
professionalCarlos19075-May-14 1:17 
Questionthought Pin
Simon_Whale2-May-14 0:47
Simon_Whale2-May-14 0:47 
AnswerRe: thought Pin
dreche5-May-14 22:25
dreche5-May-14 22:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.