Expression.Constant Expression.Constant Expression.Constant Expression.Constant Method

定義

ConstantExpression を作成します。Creates a ConstantExpression.

オーバーロード

Constant(Object) Constant(Object) Constant(Object) Constant(Object)

指定した値に設定された ConstantExpression プロパティを含む Value を作成します。Creates a ConstantExpression that has the Value property set to the specified value.

Constant(Object, Type) Constant(Object, Type) Constant(Object, Type)

指定した値に設定された ConstantExpression プロパティおよび Value プロパティを含む Type を作成します。Creates a ConstantExpression that has the Value and Type properties set to the specified values.

Constant(Object) Constant(Object) Constant(Object) Constant(Object)

指定した値に設定された ConstantExpression プロパティを含む Value を作成します。Creates a ConstantExpression that has the Value property set to the specified value.

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

パラメーター

value
Object Object Object Object

Object プロパティを等しく設定する ValueAn Object to set the Value property equal to.

戻り値

ConstantExpression と等しい NodeType プロパティおよび指定した値に設定された Constant プロパティを含む ValueA ConstantExpression that has the NodeType property equal to Constant and the Value property set to the specified value.

次のコード例では、定数値を表す式を作成する方法を示します。The following code example shows how to create an expression that represents a 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します。The Type property of the resulting ConstantExpression is equal to the type of value. 場合valuenullTypeと等しいObjectします。If value is null, Type is equal to Object.

表すnull、使用することも、Constant(Object, Type)メソッドを使用する型を明示的に指定できます。To represent null, you can also use the Constant(Object, Type) method, with which you can explicitly specify the type.

Constant(Object, Type) Constant(Object, Type) Constant(Object, Type)

指定した値に設定された ConstantExpression プロパティおよび Value プロパティを含む Type を作成します。Creates a ConstantExpression that has the Value and Type properties set to the specified values.

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

パラメーター

value
Object Object Object Object

Object プロパティを等しく設定する ValueAn Object to set the Value property equal to.

type
Type Type Type Type

Type プロパティを等しく設定する TypeA Type to set the Type property equal to.

戻り値

ConstantExpression と等しい NodeType プロパティと、指定した値に設定された Constant プロパティおよび Value プロパティを含む TypeA ConstantExpression that has the NodeType property equal to Constant and the Value and Type properties set to the specified values.

例外

valuenull ではなく、typevalue の動的型から代入することができません。value is not null and type is not assignable from the dynamic type of value.

次のコード例は、null 許容型の定数を表す式を作成し、その値に設定する方法を示しています。nullします。The following code example shows how to create an expression that represents a constant of the nullable type and set its value to 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 許容型の値を表すに役立ちます。This method can be useful for representing values of nullable types.

適用対象