This topic has not yet been rated - Rate this topic

DynamicObject.TryConvert Method

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 virtual bool TryConvert(
	ConvertBinder binder,
	out Object result
)

Parameters

binder
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.

result
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 or CTypeDynamic functions.

// Explicit conversion.
String sampleExplicit = (String)sampleObject;
// Implicit conversion.
String sampleImplicit = sampleObject;

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.

// The class derived from DynamicObject. 
public class DynamicNumber : DynamicObject
{
    // The inner dictionary.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // Getting a property. 
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        return dictionary.TryGetValue(binder.Name, out result);
    }

    // Setting a property. 
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }

    // Converting an object to a specified type. 
    public override bool TryConvert(
        ConvertBinder binder, out object result)
    {
        // Converting to string.  
        if (binder.Type == typeof(String))
        {
            result = dictionary["Textual"];
            return true;
        }

        // Converting to integer. 
        if (binder.Type == typeof(int))
        {
            result = dictionary["Numeric"];
            return true;
        }

        // In case of any other type, the binder  
        // attempts to perform the conversion itself. 
        // In most cases, a run-time exception is thrown. 
        return base.TryConvert(binder, out result);
    }
}

class Program
{
    static void Test(string[] args)
    {
        // Creating the first dynamic number.
        dynamic number = new DynamicNumber();

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

        // Implicit conversion to integer. 
        int testImplicit = number;

        // Explicit conversion to string. 
        string testExplicit = (String)number;

        Console.WriteLine(testImplicit);
        Console.WriteLine(testExplicit);

        // The following statement produces a run-time exception 
        // because the conversion to double is not implemented. 
        // double test = number;
    }
}

// This example has the following output: 

// 1 
// One

.NET Framework

Supported in: 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.