Convert.ChangeType Method (Object, TypeCode, IFormatProvider)
using System;
using System.Globalization;
public class InterceptProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(NumberFormatInfo)) {
return new System.Globalization.CultureInfo("fr-FR").NumberFormat;
}
else if (formatType == typeof(DateTimeFormatInfo)) {
return new System.Globalization.CultureInfo("en-US").DateTimeFormat;
}
else {
Console.WriteLine(" Requesting a format provider of {0}.", formatType.Name);
return null;
}
}
}
public class Example
{
public static void Main()
{
object[] values = { 103.5d, new DateTime(2010, 12, 26, 14, 34, 0) };
IFormatProvider provider = new InterceptProvider();
foreach (object value in values)
{
foreach (TypeCode enumType in ((TypeCode[]) Enum.GetValues(typeof(TypeCode))))
{
Console.WriteLine("{0} ({1}) --> {2} ({3}).",
value, value.GetType().Name,
Convert.ChangeType(value, enumType, provider),
enumType.ToString());
}
}
}
}
Related examples in the same category