Expression.Constant Metoda

Definice

Vytvoří ConstantExpression.

Přetížení

Name Description
Constant(Object)

Vytvoří ConstantExpression, který má vlastnost Value nastavenou na zadanou hodnotu.

Constant(Object, Type)

Vytvoří ConstantExpression s vlastnostmi Value a Type nastavenými na zadané hodnoty.

Constant(Object)

Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs

Vytvoří ConstantExpression, který má vlastnost Value nastavenou na zadanou hodnotu.

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

Parametry

value
Object

Object Nastavení Value vlastnosti rovno.

Návraty

AConstantExpression, který má vlastnost rovna NodeTypeConstant a Value vlastnost nastavena na zadanou hodnotu.

Příklady

Následující příklad kódu ukazuje, jak vytvořit výraz, který představuje konstantní hodnotu.

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

Poznámky

Vlastnost Type výsledného ConstantExpression se rovná typu value. Pokud je valuenull, Type se rovná Object.

Chcete-li reprezentovat null, můžete také použít Constant(Object, Type) metodu, se kterou můžete explicitně zadat typ.

Platí pro

Constant(Object, Type)

Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs
Zdroj:
ConstantExpression.cs

Vytvoří ConstantExpression s vlastnostmi Value a Type nastavenými na zadané hodnoty.

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

Parametry

value
Object

Object Nastavení Value vlastnosti rovno.

type
Type

A Type nastavit Type vlastnost rovná se.

Návraty

AConstantExpression, který má vlastnost rovna NodeTypeConstant a Value a Type vlastnosti nastaveny na zadané hodnoty.

Výjimky

type je null.

value není null a type nelze přiřadit z dynamického typu value.

Příklady

Následující příklad kódu ukazuje, jak vytvořit výraz, který představuje konstantu typu s možnou hodnotou null a nastavit jeho hodnotu na 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

Poznámky

Tato metoda může být užitečná pro reprezentaci hodnot typů s možnou hodnotou null.

Platí pro