public class GenericConverter { public static T Parse<T>(string sourceValue) where T : IConvertible { return (T)Convert.ChangeType(sourceValue, typeof(T)); } public static T Parse<T>(string sourceValue, IFormatProvider provider) where T : IConvertible { return (T)Convert.ChangeType(sourceValue, typeof(T), provider); } } public static class TConverter { public static T ChangeType<T>(object value) { return (T)ChangeType(typeof(T), value); } public static object ChangeType(Type t, object value) { TypeConverter tc = TypeDescriptor.GetConverter(t); return tc.ConvertFrom(value); } public static void RegisterTypeConverter<T, TC>() where TC : TypeConverter { TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC))); } } ----------------
Comments
Post a Comment