您的位置:首页 > 其它

System.Convert.ChangeType 转换数据类型

2008-09-18 10:41 381 查看
返回具有指定 Type 而且其值等效于指定对象的 Object

参数

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 是值类型。
ArgumentNullExceptionconversionType 为 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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: