Conversion.CTypeDynamic 方法
定义
重载
| CTypeDynamic(Object, Type) |
将对象转换为指定类型。Converts an object to the specified type. |
| CTypeDynamic<TargetType>(Object) |
将对象转换为指定的泛型类型。Converts an object to the specified generic type. |
CTypeDynamic(Object, Type)
将对象转换为指定类型。Converts an object to the specified type.
public:
static System::Object ^ CTypeDynamic(System::Object ^ Expression, Type ^ TargetType);
public static object CTypeDynamic (object? Expression, Type TargetType);
public static object CTypeDynamic (object Expression, Type TargetType);
static member CTypeDynamic : obj * Type -> obj
Public Function CTypeDynamic (Expression As Object, TargetType As Type) As Object
参数
- Expression
- Object
要转换的对象。The object to convert.
- TargetType
- Type
该对象要转换为的类型。The type to which to convert the object.
返回
一个对象,其类型在运行时为请求的目标类型。An object whose type at run time is the requested target type.
示例
下面的示例使用 CTypeDynamic 方法将动态对象转换为字符串,方法是使用由动态对象定义的转换。The following example uses the CTypeDynamic method to convert a dynamic object to a string by using the conversion defined by the dynamic object.
Imports System.Dynamic
Module Module1
Sub Main()
Dim dyn As Object = New SampleDynamicObject
Dim sampleString = CTypeDynamic(dyn, GetType(String))
Console.WriteLine(sampleString)
End Sub
End Module
Class SampleDynamicObject
Inherits DynamicObject
Public Overrides Function TryConvert(ByVal binder As ConvertBinder,
ByRef result As Object) As Boolean
If binder.Type = GetType(String) Then
result = "Sample String"
Return True
End If
Return False
End Function
End Class
注解
CTypeDynamic方法将作为参数传递的对象转换 Expression 为参数指定的类型 TargetType 。The CTypeDynamic method converts the object passed as the Expression parameter to the type specified by the TargetType parameter. 如果对象是动态对象,则该 CTypeDynamic 方法应用可用的动态转换。If the object is a dynamic object, the CTypeDynamic method applies available dynamic conversions.
CTypeDynamic方法根据对象本身定义的转换语义应用动态转换。The CTypeDynamic method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. 如果动态对象继承自 DynamicObject ,则该 CTypeDynamic 方法将首先尝试使用用户定义的静态转换执行转换。If a dynamic object inherits from DynamicObject, the CTypeDynamic method first attempts to perform the conversion by using a user-defined, static conversion. 如果用户定义的静态转换失败,该方法将 CTypeDynamic 尝试使用动态转换执行转换。If the user-defined, static conversion fails, the CTypeDynamic method attempts to perform the conversion by using dynamic conversions. 如果动态对象实现 IDynamicMetaObjectProvider ,则该 CTypeDynamic 方法将优先于用户定义的静态转换的动态转换。If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic method gives precedence to dynamic conversions over user-defined, static conversions.
另请参阅
- TryConvert(ConvertBinder, Object)
- BindConvert(ConvertBinder)
- CTypeDynamic<TargetType>(Object)
- 使用动态对象 (Visual Basic)Working with Dynamic Objects (Visual Basic)
- 演练:创建并使用动态对象(C# 和 Visual Basic)Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)
适用于
CTypeDynamic<TargetType>(Object)
将对象转换为指定的泛型类型。Converts an object to the specified generic type.
public:
generic <typename TargetType>
static TargetType CTypeDynamic(System::Object ^ Expression);
public static TargetType CTypeDynamic<TargetType> (object? Expression);
public static TargetType CTypeDynamic<TargetType> (object Expression);
static member CTypeDynamic : obj -> 'argetType
Public Function CTypeDynamic(Of TargetType) (Expression As Object) As TargetType
类型参数
- TargetType
该对象要转换为的类型。The type to which to convert the object.
参数
- Expression
- Object
要转换的对象。The object to convert.
返回
- TargetType
静态类型化为请求的泛型类型的对象。An object statically typed as the requested generic type.
示例
下面的示例使用 CTypeDynamic 方法将动态对象转换为字符串,方法是使用由动态对象定义的转换。The following example uses the CTypeDynamic method to convert a dynamic object to a string by using the conversion defined by the dynamic object.
Imports System.Dynamic
Module Module1
Sub Main()
Dim dyn As Object = New SampleDynamicObject
Dim str = CTypeDynamic(Of String)(dyn)
Console.WriteLine(str)
End Sub
End Module
Class SampleDynamicObject
Inherits DynamicObject
Public Overrides Function TryConvert(ByVal binder As ConvertBinder,
ByRef result As Object) As Boolean
If binder.Type = GetType(String) Then
result = "Sample String"
Return True
End If
Return False
End Function
End Class
注解
CTypeDynamic方法将作为参数传递的对象转换 Expression 为泛型参数类型所指定的类型。The CTypeDynamic method converts the object passed as the Expression parameter to the type specified by the type of the generic parameter. 如果对象是动态对象,则该 CTypeDynamic 方法应用可用的动态转换。If the object is a dynamic object, the CTypeDynamic method applies available dynamic conversions.
CTypeDynamic方法根据对象本身定义的转换语义应用动态转换。The CTypeDynamic method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. 如果动态对象继承自 DynamicObject ,则该 CTypeDynamic 方法将首先尝试使用用户定义的静态转换执行转换。If a dynamic object inherits from DynamicObject, the CTypeDynamic method first attempts to perform the conversion by using a user-defined, static conversion. 如果用户定义的静态转换失败,该方法将 CTypeDynamic 尝试使用动态转换执行转换。If the user-defined, static conversion fails, the CTypeDynamic method attempts to perform the conversion by using dynamic conversions. 如果动态对象实现 IDynamicMetaObjectProvider ,则该 CTypeDynamic 方法将优先于用户定义的静态转换的动态转换。If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic method gives precedence to dynamic conversions over user-defined, static conversions.
另请参阅
- TryConvert(ConvertBinder, Object)
- BindConvert(ConvertBinder)
- CTypeDynamic(Object, Type)
- 使用动态对象 (Visual Basic)Working with Dynamic Objects (Visual Basic)
- 演练:创建并使用动态对象(C# 和 Visual Basic)Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)