Expression.TypeAs(Expression, Type) Método

Definición

Crea un objeto UnaryExpression que representa una referencia explícita o conversión boxing donde se suministra null si se produce un error de conversión.

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

Parámetros

expression
Expression

Objeto Expression en el que se va a establecer la propiedad Operand.

type
Type

Type en el que se va a establecer la propiedad Type.

Devoluciones

UnaryExpression cuya propiedad NodeType es TypeAs y cuyas propiedades Operand y Type se establecen en los valores especificados.

Excepciones

expression o type es null.

Ejemplos

En el ejemplo siguiente se muestra cómo usar el TypeAs(Expression, Type) método para crear un UnaryExpression objeto que representa la conversión de referencia de una expresión de entero que no acepta valores NULL al tipo entero que acepta valores 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)

Comentarios

La Method propiedad del resultante UnaryExpression es null. Las IsLifted propiedades y IsLiftedToNull son .false

Se aplica a