Поделиться через


Expression.Constant Метод

Определение

Создает объект ConstantExpression.

Перегрузки

Имя Описание
Constant(Object)

ConstantExpression Создает свойство с Value заданным значением.

Constant(Object, Type)

Создает объект ConstantExpression с Value заданными значениями и Type свойствами.

Constant(Object)

Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs

ConstantExpression Создает свойство с Value заданным значением.

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

Параметры

value
Object

Значение Object , равное свойству Value .

Возвращаемое значение

Значение ConstantExpression , равное NodeType свойству Constant и Value свойству, заданному указанному значению.

Примеры

В следующем примере кода показано, как создать выражение, представляющее постоянное значение.

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

Комментарии

Свойство Type результирующего ConstantExpression объекта равно типу value. Если value имеет значение null, Type равно Object.

Для представления nullможно также использовать Constant(Object, Type) метод, с помощью которого можно явно указать тип.

Применяется к

Constant(Object, Type)

Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs
Исходный код:
ConstantExpression.cs

Создает объект ConstantExpression с Value заданными значениями и Type свойствами.

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

Параметры

value
Object

Значение Object , равное свойству Value .

type
Type

Значение, Type равное свойству Type .

Возвращаемое значение

Значение ConstantExpression , равное свойству NodeTypeConstant и ValueType свойствам, заданным указанным значениям.

Исключения

type равно null.

value не nulltype является и не может назначаться из динамического valueтипа.

Примеры

В следующем примере кода показано, как создать выражение, представляющее константу типа, допускающего значение NULL, и задать для нее значение 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

Комментарии

Этот метод может быть полезным для представления значений типов, допускающих значение NULL.

Применяется к