Expression.Constant 方法

定义

创建一个 ConstantExpression

重载

Constant(Object)

创建一个 ConstantExpression,它把 Value 属性设置为指定值。

Constant(Object, Type)

创建一个 ConstantExpression,它把 ValueType 属性设置为指定值。

Constant(Object)

Source:
ConstantExpression.cs
Source:
ConstantExpression.cs
Source:
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。 如果 valuenullType 则等于 Object

若要表示 null,还可以使用 Constant(Object, Type) 方法,通过该方法可以显式指定 类型。

适用于

Constant(Object, Type)

Source:
ConstantExpression.cs
Source:
ConstantExpression.cs
Source:
ConstantExpression.cs

创建一个 ConstantExpression,它把 ValueType 属性设置为指定值。

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,其 NodeType 属性等于 Constant,并且其 ValueType 属性设置为指定值。

例外

typenull

value 不为 null 并且 type 不可从 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 类型的值。

适用于