The System.Convert class in .Net is a great way to convert basic values to new and wonderful types. Unfortunately it only works on a very limited set of data types. To be precise, it only works if the type implements IConvertible and you can see what types are supported in the listing below.
public interface IConvertible
bool ToBoolean(IFormatProvider provider);
byte ToByte(IFormatProvider provider);
char ToChar(IFormatProvider provider);
DateTime ToDateTime(IFormatProvider provider);
decimal ToDecimal(IFormatProvider provider);
double ToDouble(IFormatProvider provider);
short ToInt16(IFormatProvider provider);
int ToInt32(IFormatProvider provider);
long ToInt64(IFormatProvider provider);
sbyte ToSByte(IFormatProvider provider);
float ToSingle(IFormatProvider provider);
string ToString(IFormatProvider provider);
object ToType(Type conversionType, IFormatProvider provider);
ushort ToUInt16(IFormatProvider provider);
uint ToUInt32(IFormatProvider provider);
ulong ToUInt64(IFormatProvider provider);
If you are looking to convert to other values or the type you are converting from does not implement IConvertible, you are out of luck.
The BizArk framework provides the Redwerb.BizArk.Core.ConvertEx class which extends the conversion capabilities of the .Net framework. ConvertEx can convert types based on the following conversion strategies:
- TypeConverter: This is a class that you can implement to support conversions between any data types you wish to support. You can get a TypeConverter by calling TypeDescriptor.GetConverter(Type). TypeConverters are used in the binding support in WinForms as well as a few other places in the .Net framework.
- ToXxx methods: This is a common naming convention for converting an object into another type. The most common of these is the System.Object.ToString() method.
- Parameterized constructors: This is a convention where a constructor for a class takes a typed parameter.
- Parse static methods. This is a common convention for converting strings to a particular type. See Int32.Parse for an example.
- Image to byte array and vice versa: This is a specific type of conversion to convert an image to a byte array for storage or sending to a stream.
- To/from null: Reference types can be set to null, but value types cannot. In those cases ConvertEx supports the concept of empty values. An empty value is determined based the following criteria:
- Reference type: null
- Char: 0 (that is \0)
- Static [Type].Empty property (if defined)
- Static [Type].MinValue property (if defined)
- Custom empty value. Register the empty value by calling ConvertEx.RegisterEmptyValue.
To use ConvertEx, simply call one of the ConvertEx methods. There are a number of helper methods to convert values to basic types such as integer, string, boolean, etc. However, you can also call the ConvertEx.ChangeType method to convert between any two types (as long as one of the types supports conversions from the other type).
The following example shows how to use the generic version of ConvertEx.ChangeType. As you can see, it is pretty straight forward.
var pt = new Point(5, 10);
var myPt = ConvertEx.ChangeType<MyPoint>(pt);
ConvertEx also provides a plugin architecture if you want to extend the types that it can convert to/from. To use it, just create a class that implements the Redwerb.BizArk.Core.Convert.IConvertStrategy interface and register it by calling Redwerb.BizArk.Core.Convert.ConvertStrategyMgr.SetStrategy (you must register it for each to/from type you want it to support).
BizArk is available for download on the CodePlex website, http://bizark.codeplex.com.