Expression.Convert Method (Expression, Type)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Creates a UnaryExpression that represents a type conversion operation.
Namespace: System.Linq.Expressions
Assembly: System.Core (in System.Core.dll)
Syntax
'Declaration
Public Shared Function Convert ( _
expression As Expression, _
type As Type _
) As UnaryExpression
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.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | expression or type is nulla null reference (Nothing in Visual Basic). |
InvalidOperationException | No conversion operator is defined between expression.Type and type. |
Remarks
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.
Implementing Method
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 nulla null reference (Nothing in Visual Basic).
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 nulla null reference (Nothing in Visual Basic).
Lifted versus Non-Lifted
If the implementing method is not nulla null reference (Nothing in Visual Basic):
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 nulla null reference (Nothing in Visual Basic):
If both expression.Type and type are non-nullable, the node is not lifted.
Otherwise the node is lifted.
Examples
The following code example shows how to create an expression that represents a type convertion operation.
' Add the following directive to your file:
' Imports System.Linq.Expressions
' This expression represents a type convertion operation.
Dim convertExpr As Expression = Expression.Convert(
Expression.Constant(5.5),
GetType(Int16)
)
' Print the expression.
outputBlock.Text &= convertExpr.ToString() & vbCrLf
' The following statement first creates an expression tree,
' then compiles it, and then executes it.
outputBlock.Text &= Expression.Lambda(Of Func(Of Int16))(convertExpr).Compile()() & vbCrLf
' This code example produces the following output:
'
' Convert(5.5)
' 5
// 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.
outputBlock.Text += convertExpr.ToString() + "\n";
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
outputBlock.Text += Expression.Lambda<Func<Int16>>(convertExpr).Compile()() + "\n";
// This code example produces the following output:
//
// Convert(5.5)
// 5
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.