You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add Enum method to remove not defined flags. #18190
Suppose I have an enum type defined by FlagsAttributeand has an enum value that is outside the limit of the enum type. I then want to hace a way to easily remove the flags that are not defined.
This can be useful if I eg get an enum value from an external API, and all the flags are not defined in the internal enum type. I would then want clear the unsupported flags.
If you set the the new boolena value to true, unknown flags is removed. This should only be valid for enum types that are defined with FlagsAttribute, or else the function throw a exception. (By the way, why are not ToObjectgeneric to avoid having to cast like #14083 ?)
Alternative 2
publicintBitMask{get;}
It returns a bit mask that can be used like this: var cleanedEnumValue = myEnumValue & myEnumValue.BitMask;
Alternative 3
publicobjectRemoveUndefinedFlags{get;}
It returns a enum where the undefined flags have been masked away. Or if the enum is not defined with FlagsAttribute, it throw a exception.
@AlexGhiondea
If I get a flag enum value from a external API with flags that I have not defines in my local enum, the value is undefined.
I had this problem i this code where I got a FileOptions from a WIndows API. The problem is that FileOptions do not defines all flags that the API can return, so the value can be undefined.
In the code I did use a constant to clean out undefined flags. But if the enum in the future get a new flag, we have to update this magic constant.
The alternative to iterate all flags is rely slow.
Activity
[-]Method to Enum to remove not defined flags.[/-][+]Add Enum method to remove not defined flags.[/+]AlexGhiondea commentedon Nov 30, 2016
@magol thanks for the proposal!
Would you mind adding some examples of inputs / expected outputs for this API? I am trying to understand how one would use it.
magol commentedon Jan 17, 2017
@AlexGhiondea
If I get a flag enum value from a external API with flags that I have not defines in my local enum, the value is undefined.
I had this problem i this code where I got a
FileOptionsfrom a WIndows API. The problem is thatFileOptionsdo not defines all flags that the API can return, so the value can be undefined.In the code I did use a constant to clean out undefined flags. But if the enum in the future get a new flag, we have to update this magic constant.
The alternative to iterate all flags is rely slow.
magol commentedon Jan 25, 2017
dotnet/corefx#15453
stephentoub commentedon Mar 11, 2022
This hasn't had a lot of interest in years, and it's easily achieved by just defining said mask one's self. I'm going to close this. Thanks.