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

Add Enum method to remove not defined flags. #18190

Closed
magol opened this issue Aug 17, 2016 · 4 comments
Closed

Add Enum method to remove not defined flags. #18190

magol opened this issue Aug 17, 2016 · 4 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Runtime design-discussion Ongoing discussion about design without consensus
Milestone

Comments

@magol
Copy link

magol commented Aug 17, 2016

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.

Alternative solution proposals:
Alternative 1

[ComVisibleAttribute(true)]
public static object ToObject(
    Type,
    int,
    bool=false
)

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

public int BitMask { get; }

It returns a bit mask that can be used like this:
var cleanedEnumValue = myEnumValue & myEnumValue.BitMask;

Alternative 3

public object RemoveUndefinedFlags { 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.

@karelz karelz changed the title Method to Enum to remove not defined flags. Add Enum method to remove not defined flags. Sep 8, 2016
@AlexGhiondea
Copy link
Contributor

@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
Copy link
Author

magol commented 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 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.

@magol
Copy link
Author

magol commented Jan 25, 2017

dotnet/corefx#15453

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@joperezr joperezr modified the milestones: 5.0.0, Future Jul 6, 2020
@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Jul 6, 2020
@stephentoub
Copy link
Member

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.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Runtime design-discussion Ongoing discussion about design without consensus
Projects
None yet
Development

No branches or pull requests

7 participants