-
Notifications
You must be signed in to change notification settings - Fork 158
Conversion
The conversion system in CUL is actually quite nice at present. It basically lets you convert almost any type into any other type. It does this by doing a few checks to see what the two types are and figuring out the best way to change the data from type A to type B. In order to access this system, you simply do the following:
int Result=1.4f.To(0);
The To extension function attaches itself to all objects and is in the Utilities.DataTypes namespace. The above code will attempt to convert the float to an int. For all simple types, it will simply use built in converters in .Net. String to int, float to decimal, int to enum, etc. are all covered using these built in converters.
At this point you may be asking why I passed in a 0 in the above code to the To function. The reason for this is you can specify a default. If the function fails to convert for any reason, it will return the default value that you specify. For instance if you attempt to convert a null to an integer, it will return whatever value is set to the default. If one is not specified, it returns the default for that type (so 0 in the case of ints).
Now for class conversions, it takes a bit more work on the part of the system but it does a pretty good job at this point. For instance:
IMyTestClass Result=new MyTestClass().To<MyTestClass, IMyTestClass>();
In the code above, assume that MyTestClass inherits from IMyTestClass. In this case the code will automatically cast it to IMyTestClass and that's it. It will also do this when the type is not known before hand. For instance:
IMyTestClass Result=((object)new MyTestClass()).To<object, IMyTestClass>();
In the code above, even though the object has been cast to an object, it will still detect that the object is a MyTestClass object and simply cast it and return the result.
However you may want to convert from one class type to one that it doesn't inherit from. For instance:
MyTestClass2 Result=new MyTestClass().To<MyTestClass, MyTestClass2>();
In this example, let's assume that the two class types aren't related. In this instance, the system creates a new MyTestClass2 object using the default constructor. Then, using the DataMapper system built into CUL, it determines if you have set up a mapping between the two classes. If you have set one up, it uses that. If not, it automatically sets up a mapping that does a shallow copy of the properties between the two objects. This comes in quite handy when working with any sort of DTO, view models, etc. as it cuts down on glue code.
Now there are some special cases for the To extension for more complex data type conversions. For instance, IEnumerable where T is a class can be converted to a DataTable. Similarly DataTables can be converted to an IEnumerable. Another example is the FileInfo class from the Utilities.IO namespace has many To functions for converting the class to one of the built in file types (CSV, Excel, etc).
What this comes down to is if you want to convert from type A to type B and there isn't an obvious, 1 line of code to do it, try the To extension.