Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,12 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
? operandType.GetEnumUnderlyingType()
: operandType;

if (operandType.IsEnum)
//
// Arithmetic and bitwise operators apply to the underlying type of an enum.
// A conversion takes the enum itself, so that converting it to object boxes the enum
// rather than its underlying value.
//
if (operandType.IsEnum && expr.UnaryType != UnaryType.Convert)
operand = Expression.Convert(operand, underlyingType);

switch (expr.UnaryType, operand)
Expand Down
15 changes: 15 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,18 @@ Not((1 > 0))

!!false
Not(Not(False))

#######################################
# Enum conversions

DayOfWeek.Friday :> object
Convert(DayOfWeek.Friday, Object)

1 :> DayOfWeek :> object
Convert(Convert(1, DayOfWeek), Object)

DayOfWeek.Friday :> long
Convert(DayOfWeek.Friday, Int64)

DayOfWeek.Friday :> DayOfWeek
DayOfWeek.Friday
2 changes: 2 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ private static List<object[]> EvaluatesSource
list.Add([""" 0x8000000000000000""", 0x8000000000000000L]);
list.Add(["""ulong.MaxValue""", ulong.MaxValue]);
list.Add(["""Guid.Empty""", Guid.Empty]);
list.Add(["""1 :> DayOfWeek :> object""", DayOfWeek.Monday]);
list.Add(["""(DayOfWeek.Friday :> object).ToString()""", "Friday"]);
return list;
}
}
Expand Down
Loading