System.Convert.ChangeType 转换数据类型
2008-09-18 10:41
381 查看
返回具有指定 Type 而且其值等效于指定对象的 Object。
类型:System..::.Object
实现 IConvertible 接口的 Object。
conversionType
类型:System..::.Type
Type。
一个对象,它的 Type 为 conversionType,而且它的值等效于 value。
- 或 -
如果 value 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 且 conversionType 不是值类型,则为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
异常
备注
此方法将当前线程的区域性用于转换。
using System;
public class ChangeTypeTest {
public static void Main() {
Double d = -2.345;
int i = (int)Convert.ChangeType(d, typeof(int));
Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);
string s = "12/12/98";
DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));
Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);
}
}
参数
value类型:System..::.Object
实现 IConvertible 接口的 Object。
conversionType
类型:System..::.Type
Type。
返回值
类型:System..::.Object一个对象,它的 Type 为 conversionType,而且它的值等效于 value。
- 或 -
如果 value 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 且 conversionType 不是值类型,则为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
异常
异常 | 条件 |
---|---|
InvalidCastException | 不支持此转换。 - 或 - value 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),而且 conversionType 是值类型。 |
ArgumentNullException | conversionType 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 |
备注
此方法将当前线程的区域性用于转换。
using System;
public class ChangeTypeTest {
public static void Main() {
Double d = -2.345;
int i = (int)Convert.ChangeType(d, typeof(int));
Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);
string s = "12/12/98";
DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));
Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);
}
}
相关文章推荐
- 强化System.Convert.ChangeType, 使其能够转换枚举ENUM和可空类型Nullable
- C#通用类型转换 Convert.ChangeType
- C#中使用System.Convert类将一种基础数据类型转换为其他基础数据类型
- C#通用类型转换 Convert.ChangeType
- C#通用类型转换 Convert.ChangeType
- C#通用类型转换 Convert.ChangeType
- C# Convert.ChangeType() 将未知类型转换为已知类型
- Cannot convert type 'ASP.login_aspx' to 'System.Web.UI.WebControls.Login'(无法将类型'ASP.login_aspx' 转换为'System.Web.UI.WebControls.Lo
- .NET中三种数据类型转换的区别:(type), type.Parse, Convert类
- 不允许从数据类型 ntext 到 varchar 的隐性转换。请使用 CONVERT 函数来运行此查询
- LabVIEW中的数据类型转换 Typecast In LabVIEW
- CONVERT 函数 [数据类型转换]
- 由"Type mismatch: cannot convert from View to ProgressBar"联想到的类型转换错误的根源
- SSH框架问题——hbm.xml配置出现的org.hibernate.MappingException: Could not determine type for:(貌似叫数据类型转换错误)问题
- SQL Server CONVERT() 日期转换为新数据类型的 通用函数
- Convert.ChangeType转换泛型的性能损失测试
- 第一节 4Convert简单数据类型转换
- ADO 数据类型转换表 oledbtype sqldbtype
- 用Convert类实现数据类型转换
- .NET中三种数据类型转换的区别:(type), type.Parse, Convert类