-
-
-
-
-
- class Program
- {
- static void Main(string[] args)
- {
-
-
- SingleDemo();
- Int32Demo();
- Console.ReadKey();
- }
-
-
-
- public static void SingleDemo()
- {
- String fInput = "1.23456";
- Single fOutputFromTypeString = (System.Single)ConvertFromString("System.Single", fInput);
- Single fOutputFromType = (System.Single)ConvertFromString(typeof(System.Single), fInput);
- Single fOutputFromTypeConverter = (System.Single)ConvertFromString(new SingleConverter(), fInput);
- Console.WriteLine("--------------------------------------");
- Console.WriteLine("{0} Demo", "System.Single");
- Console.WriteLine("--------------------------------------");
- Console.WriteLine("OutputFromTypeString:{0}", fOutputFromTypeString);
- Console.WriteLine("OutputFromType:{0}", fOutputFromType);
- Console.WriteLine("OutputFromTypeConverter:{0}", fOutputFromTypeConverter);
- }
-
-
-
- public static void Int32Demo()
- {
- String fInput = "123456";
- Int32 fOutputFromTypeString = (System.Int32)ConvertFromString("System.Int32", fInput);
- Int32 fOutputFromType = (System.Int32)ConvertFromString(typeof(System.Int32), fInput);
- Int32 fOutputFromTypeConverter = (System.Int32)ConvertFromString(new Int32Converter(), fInput);
- Console.WriteLine("--------------------------------------");
- Console.WriteLine("{0} Demo", "System.Int32");
- Console.WriteLine("--------------------------------------");
- Console.WriteLine("OutputFromTypeString:{0}", fOutputFromTypeString);
- Console.WriteLine("OutputFromType:{0}", fOutputFromType);
- Console.WriteLine("OutputFromTypeConverter:{0}", fOutputFromTypeConverter);
- }
-
-
-
-
-
- public static object ConvertFromString(String fTypeString, String fInput)
- {
- return ConvertFromString(Type.GetType(fTypeString), fInput);
- }
-
-
-
-
-
- public static object ConvertFromString(Type fType, String fInput)
- {
- return ConvertFromString(TypeDescriptor.GetConverter(fType), fInput);
- }
-
-
-
-
-
- public static object ConvertFromString(TypeConverter fTypeConverter, String fInput)
- {
- return fTypeConverter.ConvertFromString(fInput);
- }
- }
原文地址:http://blog.csdn.net/sabty/article/details/4392983