Skip to content

Add Enum method to remove not defined flags. #18190

Closed

Description

@magol

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.

Activity

  1. changed the title [-]Method to Enum to remove not defined flags.[/-] [+]Add Enum method to remove not defined flags.[/+] on Sep 8, 2016
  2. AlexGhiondea commented on Nov 30, 2016

    @AlexGhiondea
    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.

  3. magol commented on Jan 17, 2017

    @magol
    Author

    @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.

  4. magol commented on Jan 25, 2017

    @magol
    Author

    dotnet/corefx#15453

  5. transferred this issue fromdotnet/corefxon Jan 31, 2020
  6. added this to the 5.0 milestone on Jan 31, 2020
  7. added
    untriaged
    on Feb 23, 2020
  8. modified the milestones: 5.0.0, Future on Jul 6, 2020
  9. removed
    untriaged
    on Jul 6, 2020
  10. stephentoub commented on Mar 11, 2022

    @stephentoub
    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.

  11. ghost locked as resolved and limited conversation to collaborators on Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Runtimedesign-discussion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions