แก้ไข

แชร์ผ่าน


Conversion.CTypeDynamic Method

Definition

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

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

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.

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

Applies to

CTypeDynamic<TargetType>(Object)

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);
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

Type Parameters

TargetType

The type to which to convert the object.

Parameters

Expression
Object

The object to convert.

Returns

TargetType

An object statically typed as the requested generic type.

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

Applies to