Expression.Convert Method (Expression, Type)

Creates a UnaryExpression that represents a type conversion operation.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

public static UnaryExpression Convert(
	Expression expression,
	Type type
)

Parameters

expression
Type: System.Linq.Expressions.Expression

An Expression to set the Operand property equal to.

type
Type: System.Type

A Type to set the Type property equal to.

Return Value

Type: System.Linq.Expressions.UnaryExpression
A UnaryExpression that has the NodeType property equal to Convert and the Operand and Type properties set to the specified values.

ExceptionCondition
ArgumentNullException

expression or type is null.

InvalidOperationException

No conversion operator is defined between expression.Type and type.

The Method property of the resulting UnaryExpression is set to the implementing method. The IsLiftedToNull property is false. If the node is lifted, IsLifted is true. Otherwise, it is false.

The following rules determine the implementing method for the operation:

  • If either expression.Type or type is a user-defined type that defines an implicit or explicit conversion operator, the MethodInfo that represents that operator is the implementing method.

  • Otherwise:

    • If both expression.Type and type represent numeric or Boolean types, or nullable or non-nullable enumeration types, the implementing method is null.

    • If either expression.Type or type is a reference type and an explicit boxing, unboxing, or reference conversion exists from expression.Type to type, the implementing method is null.

If the implementing method is not null:

  • If expression.Type is assignable to the argument type of the implementing method and the return type of the implementing method is assignable to type, the node is not lifted.

  • If one or both of expression.Type or type is a nullable value type and the corresponding non-nullable value types are equal to the argument type and the return type of the implementing method respectively, the node is lifted.

If the implementing method is null:

  • If both expression.Type and type are non-nullable, the node is not lifted.

  • Otherwise the node is lifted.

The following code example shows how to create an expression that represents a type convertion operation.

            // Add the following directive to your file: 
            // using System.Linq.Expressions;   

            // This expression represents a type convertion operation. 
            Expression convertExpr = Expression.Convert(
                                        Expression.Constant(5.5),
                                        typeof(Int16)
                                    );

            // Print out the expression.
            Console.WriteLine(convertExpr.ToString());

            // The following statement first creates an expression tree, 
            // then compiles it, and then executes it.
            Console.WriteLine(Expression.Lambda<Func<Int16>>(convertExpr).Compile()());

            // This code example produces the following output: 
            // 
            // Convert(5.5) 
            // 5

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft