Expression.Constant Metoda

Definice

Vytvoří .ConstantExpression

Přetížení

Constant(Object)

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

Constant(Object, Type)

Vytvoří , ConstantExpression který má Value vlastnosti a Type nastavené na zadané hodnoty.

Constant(Object)

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

Vytvoří objekt ConstantExpression , který má Value vlastnost 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

Pro Object nastavení Value vlastnosti na hodnotu .

Návraty

A ConstantExpression , který má NodeType vlastnost rovnou ConstantValue a vlastnost nastavenou 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 objektu se rovná typu value. Pokud value je nullhodnota , Type rovná se hodnotě Object.

K reprezentaci nullmůžete použít také metodu Constant(Object, Type) , pomocí které můžete explicitně zadat typ.

Platí pro

Constant(Object, Type)

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

Vytvoří , ConstantExpression který má Value vlastnosti a Type nastavené 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

Pro Object nastavení Value vlastnosti na hodnotu .

type
Type

A Type , aby se Type vlastnost nastavil na hodnotu .

Návraty

A ConstantExpression , který má NodeType vlastnost rovnou Constant a Value vlastnosti a Type nastavené na zadané hodnoty.

Výjimky

type je null.

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

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