Expression.Constant Methode

Definition

Erstellt eine ConstantExpression.

Überlädt

Constant(Object)

Erstellt eine ConstantExpression, bei der die Value-Eigenschaft auf den angegebenen Wert festgelegt ist.

Constant(Object, Type)

Erstellt eine ConstantExpression, bei der die Value-Eigenschaft und die Type-Eigenschaft auf die angegebenen Werte festgelegt sind.

Constant(Object)

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

Erstellt eine ConstantExpression, bei der die Value-Eigenschaft auf den angegebenen Wert festgelegt ist.

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

Parameter

value
Object

Ein Object, auf den die Value-Eigenschaft festgelegt werden soll.

Gibt zurück

Ein ConstantExpression, bei dem die NodeType-Eigenschaft gleich Constant und die Value-Eigenschaft auf den angegebenen Wert festgelegt ist.

Beispiele

Das folgende Codebeispiel zeigt, wie Sie einen Ausdruck erstellen, der einen konstanten Wert darstellt.

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

Hinweise

Die Type -Eigenschaft des resultierenden ConstantExpression ist gleich dem Typ von value. Wenn value ist null, Type ist gleich Object.

Um darzustellen null, können Sie auch die Constant(Object, Type) -Methode verwenden, mit der Sie den Typ explizit angeben können.

Gilt für:

Constant(Object, Type)

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

Erstellt eine ConstantExpression, bei der die Value-Eigenschaft und die Type-Eigenschaft auf die angegebenen Werte festgelegt sind.

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

Parameter

value
Object

Ein Object, auf den die Value-Eigenschaft festgelegt werden soll.

type
Type

Ein Type, auf das die Type-Eigenschaft festgelegt werden soll.

Gibt zurück

Ein ConstantExpression, bei dem die NodeType-Eigenschaft gleich Constant ist und die Value-Eigenschaft sowie die Type-Eigenschaft auf die angegebenen Werte festgelegt sind.

Ausnahmen

type ist null.

value ist null, und type kann nicht auf der Basis des dynamischen Typs von value zugeordnet werden.

Beispiele

Das folgende Codebeispiel zeigt, wie Sie einen Ausdruck erstellen, der eine Konstante des Nullable-Typs darstellt, und deren Wert auf nullfestgelegt wird.

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

Hinweise

Diese Methode kann nützlich sein, um Werte von Nullable-Typen darzustellen.

Gilt für: