Expression.TypeAs(Expression, Type) 方法

定义

创建一个表示显式引用或装箱转换的 UnaryExpression(如果转换失败,则提供 null)。

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
Public Shared Function TypeAs (expression As Expression, type As Type) As UnaryExpression

参数

expression
Expression

要将 Expression 属性设置为与其相等的 Operand

type
Type

要将 Type 属性设置为与其相等的 Type

返回

UnaryExpression

一个 UnaryExpression,其 NodeType 属性等于 TypeAs,并且其 OperandType 属性设置为指定值。

例外

expressiontypenull

示例

下面的示例演示如何使用 TypeAs(Expression, Type) 该方法创建一个 UnaryExpression 表示不可为 null 整数表达式的引用转换到可为 null 的整数类型。

// 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)

注解

Method生成的UnaryExpression属性为 null. 和IsLiftedIsLiftedToNull属性都是 false

适用于