返回具有指定 Type 而且其值等效于指定对象的 Object。
参数
-
value
-
类型:
System..::.Object
实现 IConvertible 接口的 Object。
-
conversionType
-
类型:
System..::.Type
Type。
返回值
一个对象,它的 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);
}
}