Conversion.CTypeDynamic Method (Object, Type)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts an object to the specified type.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

Syntax

'Declaration
Public Shared Function CTypeDynamic ( _
    Expression As Object, _
    TargetType As Type _
) As Object
public static Object CTypeDynamic(
    Object Expression,
    Type TargetType
)

Parameters

  • TargetType
    Type: System.Type
    The type to which to convert the object.

Return Value

Type: System.Object
An object whose type at run time is the requested target type.

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.

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

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.