Expression.Constant Método

Definición

Crea una interfaz ConstantExpression.

Sobrecargas

Constant(Object)

Crea un objeto ConstantExpression que tiene la propiedad Value establecida en el valor especificado.

Constant(Object, Type)

Crea un objeto ConstantExpression que tiene las propiedades Value y Type establecidas en los valores especificados.

Constant(Object)

Crea un objeto ConstantExpression que tiene la propiedad Value establecida en el valor especificado.

public:
 static System::Linq::Expressions::ConstantExpression ^ Constant(System::Object ^ value);
public static System.Linq.Expressions.ConstantExpression Constant (object value);
public static System.Linq.Expressions.ConstantExpression Constant (object? value);
static member Constant : obj -> System.Linq.Expressions.ConstantExpression
Public Shared Function Constant (value As Object) As ConstantExpression

Parámetros

value
Object

Objeto Object en el que se va a establecer la propiedad Value.

Devoluciones

ConstantExpression

ConstantExpression cuya propiedad NodeType es Constant y cuya propiedad Value se establece en el valor especificado.

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear una expresión que representa un valor constante.

// Add the following directive to your file:
// using System.Linq.Expressions;

// This expression represents a Constant value.
Expression constantExpr = Expression.Constant(5.5);

// Print out the expression.
Console.WriteLine(constantExpr.ToString());

// You can also use variables.
double num = 3.5;
constantExpr = Expression.Constant(num);
Console.WriteLine(constantExpr.ToString());

// This code example produces the following output:
//
// 5.5
// 3.5
' Add the following directive to your file:
' Imports System.Linq.Expressions 

' This expression represents a constant value.
Dim constantExpr As Expression = Expression.Constant(5.5)

' Print the expression.
Console.WriteLine(constantExpr.ToString())

' You can also use variables.
Dim num As Double = 3.5
constantExpr = Expression.Constant(num)
Console.WriteLine(constantExpr.ToString())

' This code example produces the following output:
'
' 5.5
' 3.5

Comentarios

La Type propiedad del resultante ConstantExpression es igual al tipo de value. Si value es null, Type es igual a Object.

Para representar null, también puede usar el método , con el Constant(Object, Type) que puede especificar explícitamente el tipo .

Se aplica a

Constant(Object, Type)

Crea un objeto ConstantExpression que tiene las propiedades Value y Type establecidas en los valores especificados.

public:
 static System::Linq::Expressions::ConstantExpression ^ Constant(System::Object ^ value, Type ^ type);
public static System.Linq.Expressions.ConstantExpression Constant (object value, Type type);
public static System.Linq.Expressions.ConstantExpression Constant (object? value, Type type);
static member Constant : obj * Type -> System.Linq.Expressions.ConstantExpression
Public Shared Function Constant (value As Object, type As Type) As ConstantExpression

Parámetros

value
Object

Objeto Object en el que se va a establecer la propiedad Value.

type
Type

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

Devoluciones

ConstantExpression

ConstantExpression cuya propiedad NodeType es Constant y cuyas propiedades Value y Type se establecen en los valores especificados.

Excepciones

type es null.

value no es null y type no se puede asignar a partir del tipo dinámico de value.

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear una expresión que representa una constante del tipo que acepta valores NULL y establece su valor en null.

// Add the following directive to your file:
// using System.Linq.Expressions;

// This expression represents a constant value,
// for which you can explicitly specify the type.
// This can be used, for example, for defining constants of a nullable type.
Expression constantExpr = Expression.Constant(
                            null,
                            typeof(double?)
                        );

// Print out the expression.
Console.WriteLine(constantExpr.ToString());

// This code example produces the following output:
//
// null
' Add the following directive to your file:
' Imports System.Linq.Expressions   

' This expression represents a constant value, 
' for which you can explicitly specify the type. 
' This can be used, for example, for defining constants of a nullable type.
Dim constantExpr As Expression = Expression.Constant(
                            Nothing,
                            GetType(Double?)
                        )

' Print the expression.
Console.WriteLine(constantExpr.ToString())

' This code example produces the following output:
'
' null

Comentarios

Este método puede ser útil para representar valores de tipos que aceptan valores NULL.

Se aplica a