Possible Duplicate:
How can I fix this up to do generic conversion to Nullable<T>?
public static class ObjectExtensions
{
public static T To<T>(this object value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
}
My above extension method helps converting a type to another type but it doesn't support nullable types.
For example, {0} works fine but {1} doesn't work:
{0}:
var var1 = "12";
var var1Int = var1.To<int>();
{1}:
var var2 = "12";
var var2IntNullable = var2.To<int?>();
So, how to write a generic conversion method which would support converting to and from nullable types?
Thanks,