Skip to content

Handle bitwise operations on Enums#237

Merged
metoule merged 1 commit intodynamicexpresso:masterfrom
metoule:fix_235
Jun 1, 2022
Merged

Handle bitwise operations on Enums#237
metoule merged 1 commit intodynamicexpresso:masterfrom
metoule:fix_235

Conversation

@metoule
Copy link
Copy Markdown
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
@metoule metoule requested a review from davideicardi as a code owner June 1, 2022 11:19
Copy link
Copy Markdown
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