Fix bytewise operators to work on enum types with 'System.Byte' as the underlying type#2411
Fix bytewise operators to work on enum types with 'System.Byte' as the underlying type#2411lzybkr merged 1 commit intoPowerShell:masterfrom
Conversation
…e underlying type
|
Hi @daxian-dbw, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
TTYL, MSBOT; |
| expr = expr.Cast(target.LimitType); | ||
| } | ||
|
|
||
| expr = expr.Cast(typeof(object)); |
There was a problem hiding this comment.
Why was this cast moved outside of the IsEnum code? None of the tests you wrote demonstrate the need here.
There was a problem hiding this comment.
.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.
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.Int32as underlying type, then the cast of(int-enum)(object)(int)works in C#.But if the target enum type has
System.Byteas 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.