Binder.ChangeType(Object, Type, CultureInfo) 方法

定义

将给定 Object 的类型更改为给定 TypeChanges the type of the given Object to the given Type.

public:
 abstract System::Object ^ ChangeType(System::Object ^ value, Type ^ type, System::Globalization::CultureInfo ^ culture);
public abstract object ChangeType (object value, Type type, System.Globalization.CultureInfo? culture);
public abstract object ChangeType (object value, Type type, System.Globalization.CultureInfo culture);
abstract member ChangeType : obj * Type * System.Globalization.CultureInfo -> obj
Public MustOverride Function ChangeType (value As Object, type As Type, culture As CultureInfo) As Object

参数

value
Object

要更改为新 Type 的对象。The object to change into a new Type.

type
Type

value 将变成的新 TypeThe new Type that value will become.

culture
CultureInfo

一个 CultureInfo 实例,用于控制数据类型的强制转换。An instance of CultureInfo that is used to control the coercion of data types. 如果 culturenull,则使用当前线程的 CultureInfoIf culture is null, the CultureInfo for the current thread is used.

返回

Object

一个包含作为新类型的给定值的对象。An object that contains the given value as the new type.

注解

反射对通用类型系统的可访问性规则建模。Reflection models the accessibility rules of the common type system. 例如,如果调用方在同一程序集中,则调用方不需要内部成员的特殊权限。For example, if the caller is in the same assembly, the caller does not need special permissions for internal members. 否则,调用方需要 ReflectionPermissionOtherwise, the caller needs ReflectionPermission. 这与查找受保护的、私有的成员等的成员一致。This is consistent with lookup of members that are protected, private, and so on.

一般原则是 ChangeType 只应执行不会丢失数据的扩大强制。The general principle is that ChangeType should perform only widening coercions, which never lose data. 扩大强制的一个示例是将一个值为32位有符号整数的值强制为64位有符号整数值。An example of a widening coercion is coercing a value that is a 32-bit signed integer to a value that is a 64-bit signed integer. 这与收缩强制不同,后者可能会丢失数据。This is distinguished from a narrowing coercion, which may lose data. 收缩强制的一个示例是将64位有符号整数强制转换为32位有符号整数。An example of a narrowing coercion is coercing a 64-bit signed integer to a 32-bit signed integer.

下表列出了默认执行的强制转换 ChangeTypeThe following table lists the coercions performed by the default ChangeType.

源类型Source type 目标类型Target type
任何类型Any type 其基类型。Its base type.
任何类型Any type 它实现的接口。The interface it implements.
CharChar UInt16、UInt32、Int32、UInt64、Int64、Single、DoubleUInt16, UInt32, Int32, UInt64, Int64, Single, Double
ByteByte Char、UInt16、Int16、UInt32、Int32、UInt64、Int64、Single、DoubleChar, UInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double
SByteSByte Int16、Int32、Int64、Single、DoubleInt16, Int32, Int64, Single, Double
UInt16UInt16 UInt32、Int32、UInt64、Int64、Single、DoubleUInt32, Int32, UInt64, Int64, Single, Double
Int16Int16 Int32、Int64、Single、DoubleInt32, Int64, Single, Double
UInt32UInt32 UInt64、Int64、Single、DoubleUInt64, Int64, Single, Double
Int32Int32 Int64、Single、DoubleInt64, Single, Double
UInt64UInt64 Single、DoubleSingle, Double
Int64Int64 Single、DoubleSingle, Double
SingleSingle DoubleDouble
非引用Non-reference 按引用。By-reference.

如果联编程序实现允许将字符串值强制转换为数值类型,则 culture 参数对于将表示1000的字符串转换为值是必需的 Double ,因为不同的区域性以不同的方式表示1000。If a binder implementation allows coercion of string values to numeric types, the culture parameter is necessary to convert a string that represents 1000 to a Double value, because 1000 is represented differently by different cultures. 默认联编程序不进行此类字符串强制。The default binder does not do such string coercions.

适用于