Conversion.CTypeDynamic Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| CTypeDynamic(Object, Type) |
Converts an object to the specified type. |
| CTypeDynamic<TargetType>(Object) |
Converts an object to the specified generic type. |
CTypeDynamic(Object, Type)
- Source:
- Conversion.vb
- Source:
- Conversion.vb
- Source:
- Conversion.vb
- Source:
- Conversion.vb
Converts an object to the specified type.
public:
static System::Object ^ CTypeDynamic(System::Object ^ Expression, Type ^ TargetType);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The Expression's underlying type cannot be statically analyzed and its members may be trimmed")]
public static object CTypeDynamic(object? Expression, Type TargetType);
public static object CTypeDynamic(object? Expression, Type TargetType);
public static object CTypeDynamic(object Expression, Type TargetType);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The Expression's underlying type cannot be statically analyzed and its members may be trimmed")>]
static member CTypeDynamic : obj * Type -> obj
static member CTypeDynamic : obj * Type -> obj
Public Function CTypeDynamic (Expression As Object, TargetType As Type) As Object
Parameters
- Expression
- Object
The object to convert.
- TargetType
- Type
The type to which to convert the object.
Returns
An object whose type at run time is the requested target type.
- Attributes
Examples
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
Remarks
The CTypeDynamic method converts the object passed as the Expression parameter to the type specified by the TargetType parameter. If the object is a dynamic object, the CTypeDynamic method applies available dynamic conversions.
The CTypeDynamic method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. If a dynamic object inherits from DynamicObject, the CTypeDynamic method first attempts to perform the conversion by using a user-defined, static conversion. If the user-defined, static conversion fails, the CTypeDynamic method attempts to perform the conversion by using dynamic conversions. If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic method gives precedence to dynamic conversions over user-defined, static conversions.
See also
- TryConvert(ConvertBinder, Object)
- BindConvert(ConvertBinder)
- CTypeDynamic<TargetType>(Object)
- Working with Dynamic Objects (Visual Basic)
- Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)
Applies to
CTypeDynamic<TargetType>(Object)
- Source:
- Conversion.vb
- Source:
- Conversion.vb
- Source:
- Conversion.vb
- Source:
- Conversion.vb
Converts an object to the specified generic type.
public:
generic <typename TargetType>
static TargetType CTypeDynamic(System::Object ^ Expression);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The Expression's underlying type cannot be statically analyzed and its members may be trimmed")]
public static TargetType CTypeDynamic<TargetType>(object? Expression);
public static TargetType CTypeDynamic<TargetType>(object? Expression);
public static TargetType CTypeDynamic<TargetType>(object Expression);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The Expression's underlying type cannot be statically analyzed and its members may be trimmed")>]
static member CTypeDynamic : obj -> 'argetType
static member CTypeDynamic : obj -> 'argetType
Public Function CTypeDynamic(Of TargetType) (Expression As Object) As TargetType
Type Parameters
- TargetType
The type to which to convert the object.
Parameters
- Expression
- Object
The object to convert.
Returns
An object statically typed as the requested generic type.
- Attributes
Examples
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
Remarks
The CTypeDynamic method converts the object passed as the Expression parameter to the type specified by the type of the generic parameter. If the object is a dynamic object, the CTypeDynamic method applies available dynamic conversions.
The CTypeDynamic method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. If a dynamic object inherits from DynamicObject, the CTypeDynamic method first attempts to perform the conversion by using a user-defined, static conversion. If the user-defined, static conversion fails, the CTypeDynamic method attempts to perform the conversion by using dynamic conversions. If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic method gives precedence to dynamic conversions over user-defined, static conversions.
See also
- TryConvert(ConvertBinder, Object)
- BindConvert(ConvertBinder)
- CTypeDynamic(Object, Type)
- Working with Dynamic Objects (Visual Basic)
- Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)