谈谈Nullable的类型转换问题

发表于:2012-2-21 10:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Artech(cnblogs)    来源:51Testing软件测试网采编

  本篇文章讨论可空值类型(Nullable<T>)的转换,却确地说是如何将一种类型的值对象转换成相应的可空值。这来源于今天我们的一个成员遇到的一个小问题,我经过一些整理写了这篇文章。虽然没有什么技术含量可言,也希望对某些读者带来帮助。

  一、四种典型的类型转换方式

  对于类型转化,或者进一步地,对于像Int、Double、DateTime、String等这些原生类型之间的转化,我们具有四种典型的转换方式。如果类型之间不具有隐士转换关系存储,我们可以之间通过类型转换操作符进行显式转换,比如:

  1. double doubleValue = 3.14159265;  
  2. int intValue = (int)doubleValue;

  第二种则是借助于Convert这个静态类型的ChangeType或者ToXxx方法(Xxx代表转换的目标类型),比如:

  1. string literalValue = "123";   
  2. int intValue1 = Convert.ToInt32(literalValue);   
  3. int intValue2 = (int)Convert.ChangeType(literalValue,typeof(int));

  第三种方法为创建TypeConverter或者它的基于具体类型的若干子类,比如StringConverter、BooleanConverter、DateTimeConverter等。在使用的时候你需要先实例化相应的TypeConverter,然后调用相应的类型转换方法。比如:

  1. string literalValue = "1981-08-24";   
  2. DateTimeConverter dateTypeConverter = newDateTimeConverter();   
  3. DateTime dateTimeValue = (DateTime)dateTypeConverter.ConvertFromString(literalValue);   
  4. literalValue = "02:40:50";   
  5. TimeSpanConverter timeSpanConverter = new imeSpanConverter();   
  6. TimeSpan timeSpanValue = (TimeSpan imeSpanConverter.ConvertFromString(literalValue);

  最后一种常见的方法用在将基于某种具体类型的格式化字符串转化成对应的类型,我们可以调用具体类型的静态方法Parse或者TryParse实现类型的转换,比如:

  1. string literalValue = "1981-08-24";  
  2. DateTime dateTimeValue1 = DateTime.Parse(literalValue);  DateTime dateTimeValue2;   
  3. if (DateTime.TryParse(literalValue, out dateTimeValue2))   
  4. {   
  5. //...  
  6. }

  二、当类型转换遭遇Nullable<T>类型

  Convert几乎实现所有“兼容类型”之间的转换,也可以向Parse方法一样解析具有合法格式的字符串。但是,如果目标类型换成是Nullable<T>类型的时候,类型转换将会失败。比如我们将上面第二个例子的目标类型从int换成int?(Nullable<Int32>):

  1. string literalValue = "123";   
  2. try   
  3. {   
  4. int? intValue = (int?)Convert.ChangeType(literalValue,typeof(int?));   
  5. }   
  6. catch (InvalidCastException ex)   
  7. {   
  8. Console.WriteLine(ex.Message);   
  9. }

  类型转换错误消息会被输出:

  1. Invalid cast from 'System.String' to 'System.Nullable`1[[System.Int32, mscorlib,   
  2. Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'

  实际上,如果你调用Convert的ChangeType方法将任何类型对象转换成Nullable<T>类型,都会抛出出InvalidCastException异常,即使你将T类型转化成Nullable<T>。比如,我们将上面的例子中原数据类型换成int类型:

  1. int intValue1 = 123;   
  2. try   
  3. {   
  4. int? intValue = (int?)Convert.ChangeType(intValue1,typeof(int?));   
  5. }   
  6. catch (InvalidCastException ex)   
  7. {   
  8. Console.WriteLine(ex.Message);   
  9. }

  依然会输入类似的错误信息:

  1. Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib,   
  2. Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

  而实际上,T类型的对象是可以显式或者隐式转化成Nullable<T>对象的。也就是说,下面代码所表示的类型转换是没有问题的:

  1. int intValue1 = 123;   
  2. int? intValue2 = intValue1;   
  3. int? intValue3 = (int?)intValue1;

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号