Expression.TypeAs(Expression, Type) 方法

定義

建立 UnaryExpression,代表明確參考或 Boxing 轉換,其中若轉換失敗,則提供 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,其 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)

備註

產生的 MethodUnaryExpression 屬性為 nullIsLiftedIsLiftedToNull 屬性都是 false

適用於