Freigeben über


Expression.TypeAs(Expression, Type) Methode

Definition

Erstellt eine UnaryExpression , die einen expliziten Verweis oder eine Boxumwandlung darstellt, bei der null angegeben wird, wenn die Konvertierung fehlschlägt.

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

Parameter

expression
Expression

Eine Expression , die die Operand Eigenschaft gleich festlegen soll.

type
Type

A Type , um die Type Eigenschaft gleich festzulegen.

Gibt zurück

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

Ausnahmen

expression oder type ist null.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie die TypeAs(Expression, Type) Methode zum Erstellen einer UnaryExpression Methode verwendet wird, die die Verweiskonvertierung eines nicht nullablen ganzzahligen Ausdrucks in den Nullwert-Ganzzahltyp darstellt.

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

Hinweise

Die Method Eigenschaft der resultierenden UnaryExpression ist null. Die IsLifted Eigenschaften sind IsLiftedToNull beide false.

Gilt für: