Skip to content

Fix bytewise operators to work on enum types with 'System.Byte' as the underlying type#2411

Merged
lzybkr merged 1 commit intoPowerShell:masterfrom
daxian-dbw:enum
Oct 3, 2016
Merged

Fix bytewise operators to work on enum types with 'System.Byte' as the underlying type#2411
lzybkr merged 1 commit intoPowerShell:masterfrom
daxian-dbw:enum

Conversation

@daxian-dbw
Copy link
Copy Markdown
Member

Fix #2310

This is because we were casting the result expression to an object before casting it to the enum type.
If the target enum type has System.Int32 as underlying type, then the cast of (int-enum)(object)(int) works in C#.
But if the target enum type has System.Byte as underlying type, then the cast of (byte-enum)(object)(int) fails in C# with error "Specified cast is not valid" in FullCLR, and "Unable to cast object of type 'System.Int32' to type" in CoreCLR.

The fix is to cast the expression to the enum type before casting it to object.

@msftclas
Copy link
Copy Markdown

msftclas commented Oct 3, 2016

Hi @daxian-dbw, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Dongbo Wang). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

expr = expr.Cast(target.LimitType);
}

expr = expr.Cast(typeof(object));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why was this cast moved outside of the IsEnum code? None of the tests you wrote demonstrate the need here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

.Cast(typeof(object)) is removed from var expr = exprGenerator(...). In case the target is not a enum, the if (target.LimitType.GetTypeInfo().IsEnum) block will not run, but we still want the final expression to be converted to System.Object. That's why I removed the cast outside the IsEnum block.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, thanks.

@lzybkr lzybkr self-assigned this Oct 3, 2016
@lzybkr lzybkr merged commit 5fbde6e into PowerShell:master Oct 3, 2016
@daxian-dbw daxian-dbw deleted the enum branch October 3, 2016 22:29
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.

Binary operations on Byte enums throw exceptions in PS 5+

3 participants