Path: redb-route/redb.Route/Expressions/ConstantExpression.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/ConstantExpression.cs
----
using System;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Expressions;
///
/// A constant expression that always returns the same value regardless of the exchange.
///
public class ConstantExpression : Expression
{
private readonly object _value;
///
/// Gets the constant value held by this expression.
///
public object Value => _value;
///
/// Initializes a new instance of the class with the specified value.
///
/// The constant value.
public ConstantExpression(object value)
{
_value = value;
}
///
///
/// Thrown when the constant value cannot be converted to .
///
public override K Evaluate(IExchange exchange)
{
if (_value is K typedValue)
{
return typedValue;
}
if (_value == null)
{
return default;
}
try
{
return (K)RouteDecimal.ChangeType(_value, typeof(K));
}
catch (Exception ex)
{
throw new InvalidCastException(
$"Cannot convert constant value of type {_value.GetType().Name} to {typeof(K).Name}", ex);
}
}
///
/// Always thrown because a constant expression is read-only.
public override void SetValue(IExchange exchange, object value)
{
throw new NotSupportedException("Cannot set value on a constant expression");
}
///
public override string ToTemplateString() => _value?.ToString() ?? "";
}