如何设置枚举标志的所有位

发布于 2024-12-05 04:23:56 字数 533 浏览 4 评论 0原文

我想知道将 enum 标志的所有位设置为 1 的通用方法。 我只是想要一个 enum ,它会返回所有比较,而不管其他枚举如何。

这段代码有效;

[Flags]
public enum SomeRightEnum : uint
{
    CanDoNothing = 0,
    CanDoSomething = 1 << 0,
    CanDoSomethingElse = 1 << 1,
    CanDoYetAnotherThing = 1 << 2,
    ...
    DoEverything = 0xFFFFFFFF 
}

但在上面的代码中,由于它是 uint,所以我们设置了“F”的数量,如果它是 int,则不起作用。

因此,我将欣赏一种将 enum 标志的所有位设置为 1 的通用方法,无论数据类型如何(int、int64、<代码>uint等)

I wonder a generic way for setting all bits of enum flag to 1.
I just would like to have an enum which returns for all comparisons, regardless of other enums.

And this code works;

[Flags]
public enum SomeRightEnum : uint
{
    CanDoNothing = 0,
    CanDoSomething = 1 << 0,
    CanDoSomethingElse = 1 << 1,
    CanDoYetAnotherThing = 1 << 2,
    ...
    DoEverything = 0xFFFFFFFF 
}

But at the code above since it is uint we set the number of "F"s, it wouldn't work if it was int.

So I'll appreciate a generic way of setting all bits of enum flag to 1, regardless of the datatype (int, int64, uint etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

记忆之渊 2024-12-12 04:23:56

最简单的可能是:

enum Foo
{
  blah = 1,
  ....
  all = ~0
}

对于基于无符号的枚举:

enum Foo : uint
{
  blah = 1,
  ....
  all = ~0u;
}

Easiest is probably:

enum Foo
{
  blah = 1,
  ....
  all = ~0
}

For unsigned based enum:

enum Foo : uint
{
  blah = 1,
  ....
  all = ~0u;
}
无边思念无边月 2024-12-12 04:23:56
[Flags]
public enum MyEnum
{
    None   = 0,
    First  = 1 << 0,
    Second = 1 << 1,
    Third  = 1 << 2,
    Fourth = 1 << 3,
    All = ~(-1 << 4)
}
[Flags]
public enum MyEnum
{
    None   = 0,
    First  = 1 << 0,
    Second = 1 << 1,
    Third  = 1 << 2,
    Fourth = 1 << 3,
    All = ~(-1 << 4)
}
_蜘蛛 2024-12-12 04:23:56
internal static class Program
{
    private static void Main()
    {
        Console.WriteLine(Foo.Everything.HasFlag(Foo.None)); // False
        Console.WriteLine(Foo.Everything.HasFlag(Foo.Baz)); // True
        Console.WriteLine(Foo.Everything.HasFlag(Foo.Hello)); // True
    }
}

[Flags]
public enum Foo : uint
{
    None = 1 << 0,
    Bar = 1 << 1,
    Baz = 1 << 2,
    Qux = 1 << 3,
    Hello = 1 << 4,
    World = 1 << 5,
    Everything = Bar | Baz | Qux | Hello | World
}

这是你想要的吗?

internal static class Program
{
    private static void Main()
    {
        Console.WriteLine(Foo.Everything.HasFlag(Foo.None)); // False
        Console.WriteLine(Foo.Everything.HasFlag(Foo.Baz)); // True
        Console.WriteLine(Foo.Everything.HasFlag(Foo.Hello)); // True
    }
}

[Flags]
public enum Foo : uint
{
    None = 1 << 0,
    Bar = 1 << 1,
    Baz = 1 << 2,
    Qux = 1 << 3,
    Hello = 1 << 4,
    World = 1 << 5,
    Everything = Bar | Baz | Qux | Hello | World
}

Was this what you wanted?

白色秋天 2024-12-12 04:23:56

如果有人想知道:
我需要为 WPF 构建一个可绑定枚举转换器进行相同的操作。
由于我不知道反射中这些值的含义,因此我需要手动切换值(将它们绑定到复选框 pe)
将 Flagged 枚举的值设置为 -1 来设置所有位时出现问题。
如果将其设置为 -1 并取消标记所有值,则不会导致 0,因为所有未使用的位都不会取消标记。
这最适合我的情况。

SomeRightEnum someRightEnum = SomeRightEnum.CanDoNothing;
Type enumType = someRightEnum.GetType();
int newValue = 0;
var enumValues = Enum.GetValues(enumType).Cast<int>().Where(e => e == 1 || e % 2 == 0);
foreach (var value in enumValues)
{
    newValue |= value;
}
Console.WriteLine(newValue);

或者,如果您想要扩展方法:

public static class FlagExtensions
{
    public static TEnum AllFlags<TEnum>(this TEnum @enum)
        where TEnum : struct
    {
        Type enumType = typeof(TEnum);
        long newValue = 0;
        var enumValues = Enum.GetValues(enumType);
        foreach (var value in enumValues)
        {
            long v = (long)Convert.ChangeType(value, TypeCode.Int64);
            if(v == 1 || v % 2 == 0)
            {
               newValue |= v; 
            }
        }
        return (TEnum)Enum.ToObject(enumType , newValue);
    }
}

In case someone is wondering:
I needed to do the same building a Bindable enumconverter for WPF.
Since I don't know what the values mean in Reflection, I needed to manually been able to switch values (binding them to a checkbox p.e.)
There is a problem setting the value of a Flagged enum to -1 to set all the bits.
If you set it to -1 and you unflag all values it will not result in 0 because all unused bits are not unflagged.
This is wat worked best for my situation.

SomeRightEnum someRightEnum = SomeRightEnum.CanDoNothing;
Type enumType = someRightEnum.GetType();
int newValue = 0;
var enumValues = Enum.GetValues(enumType).Cast<int>().Where(e => e == 1 || e % 2 == 0);
foreach (var value in enumValues)
{
    newValue |= value;
}
Console.WriteLine(newValue);

Or if you would want an extension method:

public static class FlagExtensions
{
    public static TEnum AllFlags<TEnum>(this TEnum @enum)
        where TEnum : struct
    {
        Type enumType = typeof(TEnum);
        long newValue = 0;
        var enumValues = Enum.GetValues(enumType);
        foreach (var value in enumValues)
        {
            long v = (long)Convert.ChangeType(value, TypeCode.Int64);
            if(v == 1 || v % 2 == 0)
            {
               newValue |= v; 
            }
        }
        return (TEnum)Enum.ToObject(enumType , newValue);
    }
}
執念 2024-12-12 04:23:56

我发现 All = ~0 更优雅,但从 C# 7.0 开始,为了获得更高的透明度,您可以将

0b0101010111 等二进制文字与数字分隔符一起 使用_0b_0101_0101_11

因此,要为 Int32 设置所有标志,您可以选择以下可能性之一,它们都是相同的:

All = unchecked((int)0b_1111_1111_1111_1111_1111_1111_1111_1111)
All = unchecked((int)0b_11111111_11111111_11111111_11111111)
All = unchecked((int)0b11111111111111111111111111111111)

I find All = ~0 more elegant, but to have more transparency starting from C# 7.0 you can use

binary literals like 0b0101010111 together with digit separators _ like 0b_0101_0101_11.

So to set all flags for the Int32 you can chose one of the following possibilities, all of them are the same:

All = unchecked((int)0b_1111_1111_1111_1111_1111_1111_1111_1111)
All = unchecked((int)0b_11111111_11111111_11111111_11111111)
All = unchecked((int)0b11111111111111111111111111111111)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文