DynamicObject.TryConvert Method (ConvertBinder, Object)


Provides implementation for type conversion operations. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations that convert an object from one type to another.

Namespace:   System.Dynamic
Assembly:  System.Core (in System.Core.dll)

Public Overridable Function TryConvert (
	binder As ConvertBinder,
	<OutAttribute> ByRef result As Object
) As Boolean


Type: System.Dynamic.ConvertBinder

Provides information about the conversion operation. The binder.Type property provides the type to which the object must be converted. For example, for the statement (String)sampleObject in C# (CType(sampleObject, Type) in Visual Basic), where sampleObject is an instance of the class derived from the DynamicObject class, binder.Type returns the String type. The binder.Explicit property provides information about the kind of conversion that occurs. It returns true for explicit conversion and false for implicit conversion.

Type: System.Object

The result of the type conversion operation.

Return Value

Type: System.Boolean

true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)

Classes derived from the DynamicObject class can override this method to specify how a type conversion should be performed for a dynamic object. When the method is not overridden, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)

In C#, if this method is overridden, it is automatically invoked when you have an explicit or implicit conversion, as shown in the code example below.

In Visual Basic, only explicit conversion is supported. If you override this method, you call it by using the CTypeDynamic(Of TargetType) or CTypeDynamic functions.

// Explicit conversion - first variant.
Dim testExplicit1 = CTypeDynamic(Of String)(sampleObject)
// Explicit conversion - second variant.
Dim testExplicit2 = CTypeDynamic(sampleObject, GetType(String))

Assume that you need a data structure to store textual and numeric representations of numbers, and you want to define conversions of this data structure to strings and integers.

The following code example demonstrates the DynamicNumber class, which is derived from the DynamicObject class. DynamicNumber overrides the TryConvert method to enable type conversion. It also overrides the TrySetMember and TryGetMember methods to enable access to the data elements.

In this example, only conversion to strings and integers is supported. If you try to convert an object to any other type, a run-time exception is thrown.

' Add Imports System.Linq.Expressions
' to the beginning of the file.
' The class derived from DynamicObject.
Public Class DynamicNumber
    Inherits DynamicObject

    ' The inner dictionary to store field names and values.
    Dim dictionary As New Dictionary(Of String, Object)

    ' Get the property value.
    Public Overrides Function TryGetMember(
        ByVal binder As System.Dynamic.GetMemberBinder,
        ByRef result As Object) As Boolean

        Return dictionary.TryGetValue(binder.Name, result)

    End Function

    ' Set the property value.
    Public Overrides Function TrySetMember(
        ByVal binder As System.Dynamic.SetMemberBinder,
        ByVal value As Object) As Boolean

        dictionary(binder.Name) = value
        Return True

    End Function

    Public Overrides Function TryConvert(ByVal binder As System.Dynamic.ConvertBinder, ByRef result As Object) As Boolean
        ' Converting to string. 
        If binder.Type = GetType(String) Then
            result = dictionary("Textual")
            Return True
        End If

        ' Converting to integer.
        If binder.Type = GetType(Integer) Then
            result = dictionary("Numeric")
            Return True
        End If
        ' In case of any other type, the binder 
        ' attempts to perform the conversion itself.
        ' In most cases, a run-time exception is thrown.
        Return MyBase.TryConvert(binder, result)
    End Function
End Class

Sub Main()
    ' Creating the first dynamic number.
    Dim number As Object = New DynamicNumber()

    ' Creating properties and setting their values
    ' for the dynamic number.
    ' The TrySetMember method is called.
    number.Textual = "One"
    number.Numeric = 1

    ' Explicit conversion to string.
    Dim testString = CTypeDynamic(Of String)(number)

    ' Explicit conversion to integer.
    Dim testInteger = CTypeDynamic(number, GetType(Integer))

    ' The following statement produces a run-time exception
    ' because the conversion to double is not implemented.
    ' Dim testDouble = CTypeDynamic(Of Double)(number)

End Sub
' This example has the following output:

' One
' 1

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top