Condividi tramite


Expression.TypeAs(Expression, Type) Metodo

Definizione

Crea un oggetto UnaryExpression che rappresenta un riferimento esplicito o una conversione boxing in cui null viene fornito se la conversione non riesce.

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

Parametri

expression
Expression

Oggetto Expression su cui impostare la Operand proprietà uguale.

type
Type

Oggetto Type su cui impostare la Type proprietà uguale.

Restituisce

Oggetto UnaryExpression con la NodeType proprietà uguale a TypeAs e le Operand proprietà e Type impostate sui valori specificati.

Eccezioni

expression o type è null.

Esempio

Nell'esempio seguente viene illustrato come utilizzare il TypeAs(Expression, Type) metodo per creare un UnaryExpression oggetto che rappresenta la conversione di riferimento di un'espressione integer non nullable nel tipo integer nullable.

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

Commenti

La Method proprietà dell'oggetto risultante UnaryExpression è null. Le IsLifted proprietà e IsLiftedToNull sono entrambe false.

Si applica a