Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bitwise operations on Enums #237

Merged
merged 1 commit into from
Jun 1, 2022
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
19 changes: 17 additions & 2 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,10 +2562,14 @@ private Expression GenerateLessThanEqual(Expression left, Expression right)

private Expression GenerateBinary(ExpressionType binaryType, Expression left, Expression right)
{

if (IsDynamicExpression(left) || IsDynamicExpression(right))
return GenerateBinaryDynamic(binaryType, left, right);

// enum bitwise operations are not resolved properly by Linq
var bitwiseOps = new[] { ExpressionType.Or, ExpressionType.And };
if (left.Type.IsEnum && right.Type == left.Type && bitwiseOps.Contains(binaryType))
return GenerateBinaryEnums(binaryType, left, right);

// find the overloaded binary operator
string opName;

Expand Down Expand Up @@ -2607,7 +2611,7 @@ private Expression GenerateBinary(ExpressionType binaryType, Expression left, Ex
return Expression.MakeBinary(binaryType, left, right, liftToNull, operatorMethod);
}

private Expression GenerateBinaryDynamic(ExpressionType binaryType,Expression left, Expression right)
private Expression GenerateBinaryDynamic(ExpressionType binaryType, Expression left, Expression right)
{
//binary binder for dynamic type does not support AndAlso and OrElse as valid operations
if (binaryType == ExpressionType.AndAlso || binaryType == ExpressionType.OrElse)
Expand All @@ -2632,6 +2636,17 @@ private Expression GenerateBinaryDynamic(ExpressionType binaryType,Expression le
return Expression.Dynamic(binder, typeof(object), left, right);
}

private Expression GenerateBinaryEnums(ExpressionType binaryType, Expression left, Expression right)
{
var enumType = left.Type;
var underlyingType = enumType.GetEnumUnderlyingType();
left = Expression.Convert(left, underlyingType);
right = Expression.Convert(right, underlyingType);

var op = Expression.MakeBinary(binaryType, left, right);
return Expression.Convert(op, enumType);
}

private MethodData FindBinaryOperator(string operatorName, Expression left, Expression right)
{
if (operatorName == null)
Expand Down
18 changes: 17 additions & 1 deletion test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

// ReSharper disable SpecifyACultureInStringConversionExplicitly

Expand Down Expand Up @@ -528,7 +529,6 @@ public void GitHub_Issue_221_Reflection_not_allowed()
Assert.Throws<ReflectionNotAllowedException>(() => interpreter.Parse("list.SelectMany(t => t.GetMethods())"));
}


public static class Utils
{
public static List<T> Array<T>(IEnumerable<T> collection) => new List<T>(collection);
Expand All @@ -538,6 +538,22 @@ public static class Utils
public static int Any<T>(IEnumerable<T> collection) => 1;
public static int Any(IEnumerable collection) => 2;
}

[Test]
public void GitHub_Issue_235()
{
var target = new Interpreter();
target.Reference(typeof(RegexOptions));
target.Reference(typeof(DateTimeKind));

var result = target.Eval<RegexOptions>("RegexOptions.Compiled | RegexOptions.Singleline");
Assert.True(result.HasFlag(RegexOptions.Compiled));
Assert.True(result.HasFlag(RegexOptions.Singleline));

// DateTimeKind doesn't have the Flags attribute: the bitwise operation returns an integer
var result2 = target.Eval<DateTimeKind>("DateTimeKind.Local | DateTimeKind.Utc");
Assert.AreEqual((DateTimeKind)3, result2);
}
}

internal static class GithubIssuesTestExtensionsMethods
Expand Down