The transformations library is intended to cover type conversion, enumeration conversion and some additional helper methods.
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:”
int myInt = myGivenValue.ConvertTo<int>();
“Regardless of myGivenValue
's type.”
However if you read on, the solution that was adopted was a bit different:
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.
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.
There is no extensive help available for this project at this point.
Some examples of use:
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);
string valueInput = "15/02/2014";
DateTime actual = valueInput.ConvertTo<DateTime>(new DateTime(2000, 01, 01));
float? f = 123.524f;
int? i = f.ConvertTo<int>();
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:
string s = "123";
int i;
if (s.TryConvertTo<int>(out i)) { }
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.