使用类型转换器将定文本转换为指定对象 (转)

  1. /// <summary>   
  2. /// 标题:使用类型转换器进行转换   
  3. /// 作者:X.X.Y   
  4. /// 日期:2009-07-30   
  5. /// </summary>   
  6. class Program  
  7. {  
  8.     static void Main(string[] args)  
  9.     {  
  10.         // 使用类型转换器可以达到你的目的   
  11.         // 此处只做了转换,未做验证   
  12.         SingleDemo();  
  13.         Int32Demo();  
  14.         Console.ReadKey();  
  15.     }  
  16.     /// <summary>   
  17.     /// Single 转换示例   
  18.     /// </summary>   
  19.     public static void SingleDemo()  
  20.     {  
  21.         String fInput = "1.23456";  
  22.         Single fOutputFromTypeString = (System.Single)ConvertFromString("System.Single", fInput);  
  23.         Single fOutputFromType = (System.Single)ConvertFromString(typeof(System.Single), fInput);  
  24.         Single fOutputFromTypeConverter = (System.Single)ConvertFromString(new SingleConverter(), fInput);  
  25.         Console.WriteLine("--------------------------------------");  
  26.         Console.WriteLine("{0} Demo""System.Single");  
  27.         Console.WriteLine("--------------------------------------");  
  28.         Console.WriteLine("OutputFromTypeString:{0}", fOutputFromTypeString);  
  29.         Console.WriteLine("OutputFromType:{0}", fOutputFromType);  
  30.         Console.WriteLine("OutputFromTypeConverter:{0}", fOutputFromTypeConverter);  
  31.     }  
  32.     /// <summary>   
  33.     /// Int32 转换示例   
  34.     /// </summary>   
  35.     public static void Int32Demo()  
  36.     {  
  37.         String fInput = "123456";  
  38.         Int32 fOutputFromTypeString = (System.Int32)ConvertFromString("System.Int32", fInput);  
  39.         Int32 fOutputFromType = (System.Int32)ConvertFromString(typeof(System.Int32), fInput);  
  40.         Int32 fOutputFromTypeConverter = (System.Int32)ConvertFromString(new Int32Converter(), fInput);  
  41.         Console.WriteLine("--------------------------------------");  
  42.         Console.WriteLine("{0} Demo""System.Int32");  
  43.         Console.WriteLine("--------------------------------------");  
  44.         Console.WriteLine("OutputFromTypeString:{0}", fOutputFromTypeString);  
  45.         Console.WriteLine("OutputFromType:{0}", fOutputFromType);  
  46.         Console.WriteLine("OutputFromTypeConverter:{0}", fOutputFromTypeConverter);  
  47.     }  
  48.     /// <summary>   
  49.     /// 将指定文本转换为对象   
  50.     /// </summary>   
  51.     /// <param name="fTypeString">需要转换成的类型字符串</param>   
  52.     /// <param name="fInput">需要转换的文本</param>   
  53.     public static object ConvertFromString(String fTypeString, String fInput)  
  54.     {  
  55.         return ConvertFromString(Type.GetType(fTypeString), fInput);  
  56.     }  
  57.     /// <summary>   
  58.     /// 将指定文本转换为对象   
  59.     /// </summary>   
  60.     /// <param name="fType">需要转换成的类型</param>   
  61.     /// <param name="fInput">需要转换的文本</param>   
  62.     public static object ConvertFromString(Type fType, String fInput)  
  63.     {  
  64.         return ConvertFromString(TypeDescriptor.GetConverter(fType), fInput);  
  65.     }  
  66.     /// <summary>   
  67.     /// 将指定文本转换为对象   
  68.     /// </summary>   
  69.     /// <param name="fTypeConverter">转换器</param>   
  70.     /// <param name="fInput">需要转换的文本</param>   
  71.     public static object ConvertFromString(TypeConverter fTypeConverter, String fInput)  
  72.     {  
  73.         return fTypeConverter.ConvertFromString(fInput);  
  74.     }  
  75. }  

原文地址:http://blog.csdn.net/sabty/article/details/4392983

原文地址:https://www.cnblogs.com/lenmom/p/3447544.html