Expression.Constant Método

Definição

Sobrecargas

Name Description
Constant(Object)

Cria um ConstantExpression que tem a Value propriedade definida para o valor especificado.

Constant(Object, Type)

Cria um ConstantExpression que tem as Value propriedades e Type definidas para os valores especificados.

Constant(Object)

Cria um ConstantExpression que tem a Value propriedade definida para o valor especificado.

public:
 static System::Linq::Expressions::ConstantExpression ^ Constant(System::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

E Object para definir a Value propriedade igual a .

Devoluções

A ConstantExpression que tem a NodeType propriedade igual a Constant e a Value propriedade definida ao valor especificado.

Exemplos

O exemplo de código seguinte mostra como criar uma expressão que representa um 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

Observações

A Type propriedade do resultado ConstantExpression é igual ao tipo de value. Se value for null, Type é igual a Object.

Para representar null, também pode usar o Constant(Object, Type) método, com o qual pode especificar explicitamente o tipo.

Aplica-se a

Constant(Object, Type)

Cria um ConstantExpression que tem as Value propriedades e Type definidas para os 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);
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

E Object para definir a Value propriedade igual a .

type
Type

A Type para definir a Type propriedade igual a .

Devoluções

A ConstantExpression que tem a NodeType propriedade igual a Constant e as Value propriedades e Type definidas com os valores especificados.

Exceções

type é null.

value não é null e type não é atribuível a partir do tipo dinâmico de value.

Exemplos

O exemplo de código seguinte mostra como criar uma expressão que representa uma constante do tipo anulável e definir o seu valor para 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

Observações

Este método pode ser útil para representar valores de tipos anuláveis.

Aplica-se a