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

Conversation

metoule
Copy link
Contributor

@metoule metoule commented Jun 1, 2022

The Linq expression trees don't resolve the binary operators on Enums. We work around that by converting each operand to the underlying type (e.g. integers), performing the binary operation, then converting it back to the enum type:

// from 
var res = RegexOptions.Compiled | RegexOptions.Singleline;
// to 
var res = (RegexOptions)((int)RegexOptions.Compiled | (int)RegexOptions.Singleline);

Example:

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));

Fixes #235

…ying types (e.g. integers), perform the binary operation, then convert it back to the enum type.

Fixes dynamicexpresso#235
Copy link
Member

@davideicardi davideicardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@metoule metoule merged commit 055f156 into dynamicexpresso:master Jun 1, 2022
@metoule metoule deleted the fix_235 branch June 1, 2022 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enum operators support
2 participants