using System.ComponentModel;
public static object ChangeType(object value, Type conversionType)
if (conversionType == null)
throw new ArgumentNullException("conversionType");
Console.WriteLine("Type is {0}", value.GetType().Name);
if (conversionType.IsValueType)
TypeConverter converter = TypeDescriptor.GetConverter(conversionType);
Console.WriteLine("IsValid={0}", converter.IsValid(value));
if (!converter.IsValid(value))
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
NullableConverter nullableConverter = new NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
return Convert.ChangeType(value, conversionType);
public static void Main()
object o = ChangeType(tf, typeof(bool));
o = ChangeType(i, typeof(int));
o = ChangeType(c, typeof(Color));
if (o == null) Console.WriteLine("Is null");
else Console.WriteLine("not null");
Console.WriteLine("Hello World");