Expression.TypeAs(Expression, Type) Method

Definition

Creates a UnaryExpression that represents an explicit reference or boxing conversion where null is supplied if the conversion fails.

public:
 static System::Linq::Expressions::UnaryExpression ^ TypeAs(System::Linq::Expressions::Expression ^ expression, Type ^ type);
public static System.Linq.Expressions.UnaryExpression TypeAs (System.Linq.Expressions.Expression expression, Type type);
static member TypeAs : System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.UnaryExpression

Parameters

expression
Expression

An Expression to set the Operand property equal to.

type
Type

A Type to set the Type property equal to.

Returns

A UnaryExpression that has the NodeType property equal to TypeAs and the Operand and Type properties set to the specified values.

Exceptions

expression or type is null.

Examples

The following example demonstrates how to use the TypeAs(Expression, Type) method to create a UnaryExpression that represents the reference conversion of a non-nullable integer expression to the nullable integer type.

// Create a UnaryExpression that represents a
// conversion of an int to an int?.
System.Linq.Expressions.UnaryExpression typeAsExpression =
    System.Linq.Expressions.Expression.TypeAs(
        System.Linq.Expressions.Expression.Constant(34, typeof(int)),
        typeof(int?));

Console.WriteLine(typeAsExpression.ToString());

// This code produces the following output:
//
// (34 As Nullable`1)
' Create a UnaryExpression that represents a reference
' conversion of an Integer to an Integer? (a nullable Integer).
Dim typeAsExpression As System.Linq.Expressions.UnaryExpression = _
    System.Linq.Expressions.Expression.TypeAs( _
        System.Linq.Expressions.Expression.Constant(34, Type.GetType("System.Int32")), _
        Type.GetType("System.Nullable`1[System.Int32]"))

Console.WriteLine(typeAsExpression.ToString())

' This code produces the following output:
'
' (34 As Nullable`1)

Remarks

The Method property of the resulting UnaryExpression is null. The IsLifted and IsLiftedToNull properties are both false.

Applies to