Export (0) Print
Expand All

Conversion.CTypeDynamic<TargetType> Method (Object)

Converts an object to the specified generic type.

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

public static TargetType CTypeDynamic<TargetType>(
	Object Expression
)

Type Parameters

TargetType

The type to which to convert the object.

Parameters

Expression
Type: System.Object

The object to convert.

Return Value

Type: TargetType
An object statically typed as the requested generic type.

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.

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

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft