Expression.Convert Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a UnaryExpression that represents a type conversion operation.
Convert(Expression, Type) |
Creates a UnaryExpression that represents a type conversion operation. |
Convert(Expression, Type, MethodInfo) |
Creates a UnaryExpression that represents a conversion operation for which the implementing method is specified. |
- Source:
- UnaryExpression.cs
- Source:
- UnaryExpression.cs
- Source:
- UnaryExpression.cs
Creates a UnaryExpression that represents a type conversion operation.
public:
static System::Linq::Expressions::UnaryExpression ^ Convert(System::Linq::Expressions::Expression ^ expression, Type ^ type);
public static System.Linq.Expressions.UnaryExpression Convert (System.Linq.Expressions.Expression expression, Type type);
static member Convert : System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.UnaryExpression
Public Shared Function Convert (expression As Expression, type As Type) As UnaryExpression
Parameters
- expression
- Expression
An Expression to set the Operand property equal to.
Returns
A UnaryExpression that has the NodeType property equal to Convert and the Operand and Type properties set to the specified values.
Exceptions
expression
or type
is null
.
No conversion operator is defined between expression
.Type and type
.
Examples
The following code example shows how to create an expression that represents a type conversion operation.
// Add the following directive to your file:
// using System.Linq.Expressions;
// This expression represents a type conversion 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
' Add the following directive to your file:
' Imports System.Linq.Expressions
' This expression represents a type conversion operation.
Dim convertExpr As Expression = Expression.Convert(
Expression.Constant(5.5),
GetType(Int16)
)
' Print 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(Of Func(Of Int16))(convertExpr).Compile()())
' This code example produces the following output:
'
' Convert(5.5)
' 5
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 ortype
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 andtype
represent numeric or Boolean types, or nullable or non-nullable enumeration types, the implementing method isnull
.If either
expression
.Type ortype
is a reference type and an explicit boxing, unboxing, or reference conversion exists fromexpression
.Type totype
, the implementing method isnull
.
Lifted versus Non-Lifted
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 totype
, the node is not lifted.If one or both of
expression
.Type ortype
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 andtype
are non-nullable, the node is not lifted.Otherwise the node is lifted.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Source:
- UnaryExpression.cs
- Source:
- UnaryExpression.cs
- Source:
- UnaryExpression.cs
Creates a UnaryExpression that represents a conversion operation for which the implementing method is specified.
public:
static System::Linq::Expressions::UnaryExpression ^ Convert(System::Linq::Expressions::Expression ^ expression, Type ^ type, System::Reflection::MethodInfo ^ method);
public static System.Linq.Expressions.UnaryExpression Convert (System.Linq.Expressions.Expression expression, Type type, System.Reflection.MethodInfo method);
public static System.Linq.Expressions.UnaryExpression Convert (System.Linq.Expressions.Expression expression, Type type, System.Reflection.MethodInfo? method);
static member Convert : System.Linq.Expressions.Expression * Type * System.Reflection.MethodInfo -> System.Linq.Expressions.UnaryExpression
Public Shared Function Convert (expression As Expression, type As Type, method As MethodInfo) As UnaryExpression
Parameters
- expression
- Expression
An Expression to set the Operand property equal to.
- method
- MethodInfo
A MethodInfo to set the Method property equal to.
Returns
A UnaryExpression that has the NodeType property equal to Convert and the Operand, Type, and Method properties set to the specified values.
Exceptions
expression
or type
is null
.
method
is not null
and the method it represents returns void
, is not static
(Shared
in Visual Basic), or does not take exactly one argument.
No conversion operator is defined between expression
.Type and type
.
-or-
expression
.Type is not assignable to the argument type of the method represented by method
.
-or-
The return type of the method represented by method
is not assignable to type
.
-or-
expression
.Type or type
is a nullable value type and the corresponding non-nullable value type does not equal the argument type or the return type, respectively, of the method represented by method
.
More than one method that matches the method
description was found.
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 method is not
null
, it is the implementing method. It must represent a non-void,static
(Shared
in Visual Basic) method that takes one argument.Otherwise, if either
expression
.Type ortype
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 andtype
represent numeric or Boolean types, or nullable or non-nullable enumeration types, the implementing method isnull
.If either
expression
.Type ortype
is a reference type and an explicit boxing, unboxing, or reference conversion exists fromexpression
.Type totype
, the implementing method isnull
.
Lifted versus Non-Lifted
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 totype
, the node is not lifted.If either or both of
expression
.Type ortype
are 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 andtype
are non-nullable, the node is not lifted.Otherwise the node is lifted.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: