如何检查是否设置了标志组合的任何标志?

发布于 2024-08-03 12:59:09 字数 443 浏览 3 评论 0原文

假设我有这个枚举:

[Flags]
enum Letters
{
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = A | B | C,
}

要检查例如 AB 是否已设置,我可以这样做:

if((letter & Letters.AB) == Letters.AB)

是否有比以下更简单的方法来检查组合标志常量的任何标志是否已设置?

if((letter & Letters.A) == Letters.A || (letter & Letters.B) == Letters.B)

例如,可以用某些东西交换 & 吗?

Let's say I have this enum:

[Flags]
enum Letters
{
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = A | B | C,
}

To check if for example AB is set I can do this:

if((letter & Letters.AB) == Letters.AB)

Is there a simpler way to check if any of the flags of a combined flag constant are set than the following?

if((letter & Letters.A) == Letters.A || (letter & Letters.B) == Letters.B)

Could one for example swap the & with something?

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

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

发布评论

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

评论(18

尴尬癌患者 2024-08-10 12:59:09

在 .NET 4 中,您可以使用 Enum.HasFlag 方法

using System;

[Flags] public enum Pet {
   None = 0,
   Dog = 1,
   Cat = 2,
   Bird = 4,
   Rabbit = 8,
   Other = 16
}

public class Example
{
   public static void Main()
   {
      // Define three families: one without pets, one with dog + cat and one with a dog only
      Pet[] petsInFamilies = { Pet.None, Pet.Dog | Pet.Cat, Pet.Dog };
      int familiesWithoutPets = 0;
      int familiesWithDog = 0;

      foreach (Pet petsInFamily in petsInFamilies)
      {
         // Count families that have no pets. 
         if (petsInFamily.Equals(Pet.None))
            familiesWithoutPets++;
         // Of families with pets, count families that have a dog. 
         else if (petsInFamily.HasFlag(Pet.Dog))
            familiesWithDog++;
      }
      Console.WriteLine("{0} of {1} families in the sample have no pets.", 
                        familiesWithoutPets, petsInFamilies.Length);   
      Console.WriteLine("{0} of {1} families in the sample have a dog.", 
                        familiesWithDog, petsInFamilies.Length);   
   }
}

示例显示以下输出:

//       1 of 3 families in the sample have no pets. 
//       2 of 3 families in the sample have a dog.

In .NET 4 you can use the Enum.HasFlag method :

using System;

[Flags] public enum Pet {
   None = 0,
   Dog = 1,
   Cat = 2,
   Bird = 4,
   Rabbit = 8,
   Other = 16
}

public class Example
{
   public static void Main()
   {
      // Define three families: one without pets, one with dog + cat and one with a dog only
      Pet[] petsInFamilies = { Pet.None, Pet.Dog | Pet.Cat, Pet.Dog };
      int familiesWithoutPets = 0;
      int familiesWithDog = 0;

      foreach (Pet petsInFamily in petsInFamilies)
      {
         // Count families that have no pets. 
         if (petsInFamily.Equals(Pet.None))
            familiesWithoutPets++;
         // Of families with pets, count families that have a dog. 
         else if (petsInFamily.HasFlag(Pet.Dog))
            familiesWithDog++;
      }
      Console.WriteLine("{0} of {1} families in the sample have no pets.", 
                        familiesWithoutPets, petsInFamilies.Length);   
      Console.WriteLine("{0} of {1} families in the sample have a dog.", 
                        familiesWithDog, petsInFamilies.Length);   
   }
}

The example displays the following output:

//       1 of 3 families in the sample have no pets. 
//       2 of 3 families in the sample have a dog.
指尖上得阳光 2024-08-10 12:59:09

如果您想知道字母是否包含 AB 中的任何字母,则必须使用 AND & 运算符。像这样的东西:

if ((letter & Letters.AB) != 0)
{
    // Some flag (A,B or both) is enabled
}
else
{
    // None of them are enabled
}

If you want to know if letter has any of the letters in AB you must use the AND & operator. Something like:

if ((letter & Letters.AB) != 0)
{
    // Some flag (A,B or both) is enabled
}
else
{
    // None of them are enabled
}
七七 2024-08-10 12:59:09

我使用扩展方法来编写类似的东西:

if (letter.IsFlagSet(Letter.AB))
    ...

这是代码:

public static class EnumExtensions
{
    private static void CheckIsEnum<T>(bool withFlags)
    {
        if (!typeof(T).IsEnum)
            throw new ArgumentException(string.Format("Type '{0}' is not an enum", typeof(T).FullName));
        if (withFlags && !Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
            throw new ArgumentException(string.Format("Type '{0}' doesn't have the 'Flags' attribute", typeof(T).FullName));
    }

    public static bool IsFlagSet<T>(this T value, T flag) where T : struct
    {
        CheckIsEnum<T>(true);
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flag);
        return (lValue & lFlag) != 0;
    }

    public static IEnumerable<T> GetFlags<T>(this T value) where T : struct
    {
        CheckIsEnum<T>(true);
        foreach (T flag in Enum.GetValues(typeof(T)).Cast<T>())
        {
            if (value.IsFlagSet(flag))
                yield return flag;
        }
    }

    public static T SetFlags<T>(this T value, T flags, bool on) where T : struct
    {
        CheckIsEnum<T>(true);
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flags);
        if (on)
        {
            lValue |= lFlag;
        }
        else
        {
            lValue &= (~lFlag);
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }

    public static T SetFlags<T>(this T value, T flags) where T : struct
    {
        return value.SetFlags(flags, true);
    }

    public static T ClearFlags<T>(this T value, T flags) where T : struct
    {
        return value.SetFlags(flags, false);
    }

    public static T CombineFlags<T>(this IEnumerable<T> flags) where T : struct
    {
        CheckIsEnum<T>(true);
        long lValue = 0;
        foreach (T flag in flags)
        {
            long lFlag = Convert.ToInt64(flag);
            lValue |= lFlag;
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }

    public static string GetDescription<T>(this T value) where T : struct
    {
        CheckIsEnum<T>(false);
        string name = Enum.GetName(typeof(T), value);
        if (name != null)
        {
            FieldInfo field = typeof(T).GetField(name);
            if (field != null)
            {
                DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                {
                    return attr.Description;
                }
            }
        }
        return null;
    }
}

I use extension methods to write things like that :

if (letter.IsFlagSet(Letter.AB))
    ...

Here's the code :

public static class EnumExtensions
{
    private static void CheckIsEnum<T>(bool withFlags)
    {
        if (!typeof(T).IsEnum)
            throw new ArgumentException(string.Format("Type '{0}' is not an enum", typeof(T).FullName));
        if (withFlags && !Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
            throw new ArgumentException(string.Format("Type '{0}' doesn't have the 'Flags' attribute", typeof(T).FullName));
    }

    public static bool IsFlagSet<T>(this T value, T flag) where T : struct
    {
        CheckIsEnum<T>(true);
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flag);
        return (lValue & lFlag) != 0;
    }

    public static IEnumerable<T> GetFlags<T>(this T value) where T : struct
    {
        CheckIsEnum<T>(true);
        foreach (T flag in Enum.GetValues(typeof(T)).Cast<T>())
        {
            if (value.IsFlagSet(flag))
                yield return flag;
        }
    }

    public static T SetFlags<T>(this T value, T flags, bool on) where T : struct
    {
        CheckIsEnum<T>(true);
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flags);
        if (on)
        {
            lValue |= lFlag;
        }
        else
        {
            lValue &= (~lFlag);
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }

    public static T SetFlags<T>(this T value, T flags) where T : struct
    {
        return value.SetFlags(flags, true);
    }

    public static T ClearFlags<T>(this T value, T flags) where T : struct
    {
        return value.SetFlags(flags, false);
    }

    public static T CombineFlags<T>(this IEnumerable<T> flags) where T : struct
    {
        CheckIsEnum<T>(true);
        long lValue = 0;
        foreach (T flag in flags)
        {
            long lFlag = Convert.ToInt64(flag);
            lValue |= lFlag;
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }

    public static string GetDescription<T>(this T value) where T : struct
    {
        CheckIsEnum<T>(false);
        string name = Enum.GetName(typeof(T), value);
        if (name != null)
        {
            FieldInfo field = typeof(T).GetField(name);
            if (field != null)
            {
                DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                {
                    return attr.Description;
                }
            }
        }
        return null;
    }
}
会傲 2024-08-10 12:59:09

.NET 4 或更高版本中有 HasFlag 方法。

检查字母是否包含 A OR B

letter.HasFlag(Letters.A) || letter.HasFlag(B);

检查字母是否包含 A AND B

letter.HasFlag(Letters.A | Letters.B);

There is the HasFlag method in .NET 4 or higher.

Check if the letter includes A OR B

letter.HasFlag(Letters.A) || letter.HasFlag(B);

Check if the letter includes A AND B

letter.HasFlag(Letters.A | Letters.B);
诗化ㄋ丶相逢 2024-08-10 12:59:09

如果您可以使用 .NET 4 或更高版本,则使用 HasFlag() 方法

示例

letter.HasFlag(Letters.A | Letters.B) // both A and B must be set

letter.HasFlag(Letters.AB)

If you can use .NET 4 or higher than use HasFlag() method

examples

letter.HasFlag(Letters.A | Letters.B) // both A and B must be set

same as

letter.HasFlag(Letters.AB)
空气里的味道 2024-08-10 12:59:09

如果它真的让你烦恼,你可以编写这样的函数:

public bool IsSet(Letters value, Letters flag)
{
    return (value & flag) == flag;
}

if (IsSet(letter, Letters.A))
{
   // ...
}

// If you want to check if BOTH Letters.A and Letters.B are set:
if (IsSet(letter, Letters.A & Letters.B))
{
   // ...
}

// If you want an OR, I'm afraid you will have to be more verbose:
if (IsSet(letter, Letters.A) || IsSet(letter, Letters.B))
{
   // ...
}

If it really annoys you, you can write a function like that:

public bool IsSet(Letters value, Letters flag)
{
    return (value & flag) == flag;
}

if (IsSet(letter, Letters.A))
{
   // ...
}

// If you want to check if BOTH Letters.A and Letters.B are set:
if (IsSet(letter, Letters.A & Letters.B))
{
   // ...
}

// If you want an OR, I'm afraid you will have to be more verbose:
if (IsSet(letter, Letters.A) || IsSet(letter, Letters.B))
{
   // ...
}
囚你心 2024-08-10 12:59:09

要检查例如 AB 是否已设置,我可以这样做:

if((字母 & 字母.AB) == 字母.AB)

是否有比以下更简单的方法来检查组合标志常量的任何标志是否已设置?

这会检查 A 和 B 是否均已设置,并忽略是否设置了任何其他标志。

if((字母 & 字母.A) == 字母.A || (字母 & 字母.B) == 字母.B)

这会检查 A 或 B 是否已设置,并忽略是否设置了任何其他标志。

这可以简化为:

if(letter & Letters.AB)

这是用于二元运算的 C;将其应用到 C# 应该很简单:

enum {
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = AB | C,
};

int flags = A|C;

bool anything_and_a = flags & A;

bool only_a = (flags == A);

bool a_and_or_c_and_anything_else = flags & (A|C);

bool both_ac_and_anything_else = (flags & (A|C)) == (A|C);

bool only_a_and_c = (flags == (A|C));

顺便说一句,问题示例中变量的命名是单数“字母”,这可能意味着它仅代表单个字母;示例代码清楚地表明它是一组可能的字母并且允许多个值,因此请考虑重命名变量“字母”。

To check if for example AB is set I can do this:

if((letter & Letters.AB) == Letters.AB)

Is there a simpler way to check if any of the flags of a combined flag constant are set than the following?

This checks that both A and B are set, and ignores whether any other flags are set.

if((letter & Letters.A) == Letters.A || (letter & Letters.B) == Letters.B)

This checks that either A or B is set, and ignores whether any other flags are set or not.

This can be simplified to:

if(letter & Letters.AB)

Here's the C for binary operations; it should be straightforward to apply this to C#:

enum {
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = AB | C,
};

int flags = A|C;

bool anything_and_a = flags & A;

bool only_a = (flags == A);

bool a_and_or_c_and_anything_else = flags & (A|C);

bool both_ac_and_anything_else = (flags & (A|C)) == (A|C);

bool only_a_and_c = (flags == (A|C));

Incidentally, the naming of the variable in the question's example is the singular 'letter', which might imply that it represents only a single letter; the example code makes it clear that its a set of possible letters and that multiple values are allowed, so consider renaming the variable 'letters'.

一梦浮鱼 2024-08-10 12:59:09

怎么样

if ((letter & Letters.AB) > 0)

How about

if ((letter & Letters.AB) > 0)

?

不气馁 2024-08-10 12:59:09

我创建了一个简单的扩展方法,不需要检查 Enum 类型:

public static bool HasAnyFlag(this Enum value, Enum flags)
{
    return
        value != null && ((Convert.ToInt32(value) & Convert.ToInt32(flags)) != 0);
}

它也适用于可为 null 的枚举。标准的 HasFlag 方法没有,所以我也创建了一个扩展来覆盖它。

public static bool HasFlag(this Enum value, Enum flags)
{
    int f = Convert.ToInt32(flags);

    return
        value != null && ((Convert.ToInt32(value) & f) == f);
}

一个简单的测试:

[Flags]
enum Option
{
    None = 0x00,
    One = 0x01,
    Two = 0x02,
    Three = One | Two,
    Four = 0x04
}

[TestMethod]
public void HasAnyFlag()
{
    Option o1 = Option.One;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(false, o1.HasFlag(Option.Three));

    o1 |= Option.Two;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(true, o1.HasFlag(Option.Three));
}

[TestMethod]
public void HasAnyFlag_NullableEnum()
{
    Option? o1 = Option.One;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(false, o1.HasFlag(Option.Three));

    o1 |= Option.Two;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(true, o1.HasFlag(Option.Three));
}

享受吧!

I created a simple extension method that does not need a check on Enum types:

public static bool HasAnyFlag(this Enum value, Enum flags)
{
    return
        value != null && ((Convert.ToInt32(value) & Convert.ToInt32(flags)) != 0);
}

It also works on nullable enums. The standard HasFlag method does not, so I created an extension to cover that too.

public static bool HasFlag(this Enum value, Enum flags)
{
    int f = Convert.ToInt32(flags);

    return
        value != null && ((Convert.ToInt32(value) & f) == f);
}

A simple test:

[Flags]
enum Option
{
    None = 0x00,
    One = 0x01,
    Two = 0x02,
    Three = One | Two,
    Four = 0x04
}

[TestMethod]
public void HasAnyFlag()
{
    Option o1 = Option.One;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(false, o1.HasFlag(Option.Three));

    o1 |= Option.Two;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(true, o1.HasFlag(Option.Three));
}

[TestMethod]
public void HasAnyFlag_NullableEnum()
{
    Option? o1 = Option.One;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(false, o1.HasFlag(Option.Three));

    o1 |= Option.Two;
    Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
    Assert.AreEqual(true, o1.HasFlag(Option.Three));
}

Enjoy!

无妨# 2024-08-10 12:59:09

对于任何类型的枚举,您可以在枚举上使用此扩展方法:

public static bool IsSingle(this Enum value)
{
    var items = Enum.GetValues(value.GetType());
    var counter = 0;
    foreach (var item in items)
    {
        if (value.HasFlag((Enum)item))
        {
            counter++;
        }
        if (counter > 1)
        {
            return false;
        }
    }
    return true;
}

You can use this extension method on enum, for any type of enums:

public static bool IsSingle(this Enum value)
{
    var items = Enum.GetValues(value.GetType());
    var counter = 0;
    foreach (var item in items)
    {
        if (value.HasFlag((Enum)item))
        {
            counter++;
        }
        if (counter > 1)
        {
            return false;
        }
    }
    return true;
}
小霸王臭丫头 2024-08-10 12:59:09

这里有很多答案,但我认为使用标志执行此操作的最惯用方法是 Letters.AB.HasFlag(letter) 或 (Letters.A | Letters.B).HasFlag(letter) 如果您没有已经有了 Letters.AB。 letter.HasFlag(Letters.AB) 仅在同时具有两者时才有效。

There are a lot of answers on here but I think the most idiomatic way to do this with Flags would be Letters.AB.HasFlag(letter) or (Letters.A | Letters.B).HasFlag(letter) if you didn't already have Letters.AB. letter.HasFlag(Letters.AB) only works if it has both.

只是一片海 2024-08-10 12:59:09

从 .Net 4 开始,您可以使用速记版本而无需显式指定 &:

if(Letters.AB.HasFlag(Letters.C))

Starting with .Net 4, you can use a shorthand version without explicitly specifying &:

if(Letters.AB.HasFlag(Letters.C))
微凉徒眸意 2024-08-10 12:59:09

这对你有用吗?

if ((letter & (Letters.A | Letters.B)) != 0)

Would this work for you?

if ((letter & (Letters.A | Letters.B)) != 0)
花之痕靓丽 2024-08-10 12:59:09
if((int)letter != 0) { }
if((int)letter != 0) { }
少女的英雄梦 2024-08-10 12:59:09

您可以检查该值是否不为零。

if ((Int32)(letter & Letters.AB) != 0) { }

但我认为引入一个值为零的新枚举值并与该枚举值进行比较是一个更好的解决方案(如果可能的话,因为您必须能够修改枚举)。

[Flags]
enum Letters
{
    None = 0,
    A    = 1,
    B    = 2,
    C    = 4,
    AB   =  A | B,
    All  = AB | C
}

if (letter != Letters.None) { }

更新

误读了问题 - 修复了第一个建议并忽略第二个建议。

You could just check if the value is not zero.

if ((Int32)(letter & Letters.AB) != 0) { }

But I would consider it a better solution to introduce a new enumeration value with value zero and compare agains this enumeration value (if possible because you must be able to modify the enumeration).

[Flags]
enum Letters
{
    None = 0,
    A    = 1,
    B    = 2,
    C    = 4,
    AB   =  A | B,
    All  = AB | C
}

if (letter != Letters.None) { }

UPDATE

Missread the question - fixed the first suggestion and just ignore the second suggestion.

转身泪倾城 2024-08-10 12:59:09

我认为有两种方法可以用于检查是否设置了任何位。

方法 A

if (letter != 0)
{
}

只要您不介意检查所有位(包括未定义的位),此方法就有效!

方法 B

if ((letter & Letters.All) != 0)
{
}

这只检查定义的位,只要 Letters.All 代表所有可能的位。

对于特定位(一组或多组),请使用方法 B 将 Letters.All 替换为您要检查的位(见下文)。

if ((letter & Letters.AB) != 0)
{
}

There are two aproaches that I can see that would work for checking for any bit being set.

Aproach A

if (letter != 0)
{
}

This works as long as you don't mind checking for all bits, including non-defined ones too!

Aproach B

if ((letter & Letters.All) != 0)
{
}

This only checks the defined bits, as long as Letters.All represents all of the possible bits.

For specific bits (one or more set), use Aproach B replacing Letters.All with the bits that you want to check for (see below).

if ((letter & Letters.AB) != 0)
{
}
原来是傀儡 2024-08-10 12:59:09

我们能否轻松高效地查明是否至少设置了一个标志

好吧,如果您对检查是否至少设置了一个感到满意,那么是的!

用法:

if (EnumHelper.HasAnyFlagBitsSet(letter))

实现:

public static class EnumHelper
{
    static EnumHelper()
    {
        // Required to get correct behavior in GetNumericValue
        // Because we will overlap the enum type with a ulong, left-aligned
        if (!BitConverter.IsLittleEndian)
            throw new NotSupportedException("This type is only supported on little-endian architectures.");
    }

    /// <summary>
    /// <para>
    /// Returns whether the given enum value has any bits set that occurs in a defined flag for <typeparamref name="T"/>.
    /// </para>
    /// <para>
    /// Throws if the type parameter is not an enum type with the <see cref="FlagsAttribute"/>.
    /// </para>
    /// </summary>
    public static bool HasAnyFlagBitsSet<T>(T enumValue)
        where T : unmanaged, Enum
    {
        var numericValue = GetNumericValue(enumValue);

        // Take the value that has all the permitted bits set
        // Use & to keep only the corresponding bits from the input value
        // Check that the input value provided at least one such bit
        return (numericValue & FlagValueCache<T>.AllFlagsSetValue) != 0;
    }

    /// <summary>
    /// <para>
    /// Returns whether the given enum value has any bits set that are set in <paramref name="flags"/>.
    /// </para>
    /// <para>
    /// Throws if the type parameter is not an enum type with the <see cref="FlagsAttribute"/>.
    /// </para>
    /// </summary>
    public static bool HasAnyFlagBitsSet<T>(T enumValue, T flags)
        where T : unmanaged, Enum
    {
        var numericValue = GetNumericValue(enumValue);
        var numericFlags = GetNumericValue(flags);

        // Use & to keep only the bits present in flags
        // Check that the input value provided at least one such bit
        return (numericValue & flags) != 0;
    }

    // Actually, have a bonus method as well, since this is a common operation:

    /// <summary>
    /// <para>
    /// Returns whether the given enum value consists exclusively of defined flags for <typeparamref name="T"/>.
    /// The result is false if a bit is set that is not part of any value defined by <typeparamref name="T"/>.
    /// </para>
    /// <para>
    /// Throws if the type parameter is not an enum type with the <see cref="FlagsAttribute"/>.
    /// </para>
    /// </summary>
    public static bool HasDefinedFlags<T>(T enumValue)
        where T : unmanaged, Enum
    {
        var numericValue = GetNumericValue(enumValue);

        // Take the value that has all the permitted bits set
        // Use ~ to get a value with all the forbidden bits set
        // Use & to keep only the corresponding bits from the input value
        // Check that the input value provided no such forbidden bits
        return (numericValue & ~FlagValueCache<T>.AllFlagsSetValue) == 0;
    }

    /// <summary>
    /// <para>
    /// Returns the numeric value of the given <paramref name="enumValue"/>.
    /// </para>
    /// <para>
    /// The resulting <see cref="ulong"/> can be cast to the intended integral type, even if it is a signed type.
    /// </para>
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ulong GetNumericValue<T>(T enumValue)
        where T : unmanaged, Enum
    {
        Span<ulong> ulongSpan = stackalloc ulong[] { 0UL };
        Span<T> span = MemoryMarshal.Cast<ulong, T>(ulongSpan);

        span[0] = enumValue;

        return ulongSpan[0];
    }

    /// <summary>
    /// Statically caches a "full" flags value each enum type for which this class is accessed.
    /// </summary>
    internal static class FlagValueCache<T>
        where T : unmanaged, Enum
    {
        /// <summary>
        /// Each bit that is set in any of the type's defined values is also set in this value.
        /// </summary>
        public static ulong AllFlagsSetValue { get; }

        static FlagValueCache()
        {
            if (typeof(T).BaseType != typeof(Enum)) throw new Exception("The type parameter must be an enum type.");

            foreach (var value in (T[])Enum.GetValues(typeof(T)))
                AllFlagsSetValue |= GetNumericValue(value);
        }
    }
}

我们检查是否至少设置了一个标志,这意味着什么?

好吧,这个解决方案可能无法正确回答如下所示的无意义枚举:

[Flags]
public enum Nonsense
{
    One = 1,

    // Eh, why does this value need TWO bits when those bits are NOT defined as individual values?
    TwoAndFour = 2 | 4,
}

这里,EnumHelper.HasAnyFlagBitSet((Nonsense)2) 将返回 true,即 技术上不正确,因为2不是一个定义的标志。

然而,它对于所有明智的标志枚举都非常有效,包括具有多标志的枚举:

[Flags]
public enum Fine
{
    One = 1,
    Two = 2,
    Four = 4,

    // Fine, and sensible, since these flags exist individually
    TwoAndFour = 2 | 4,
}

Can we find out easily and efficiently whether at least one flag is set?

Well, if you are satisfied with checking whether at least one flag bit is set, then yes!

Usage:

if (EnumHelper.HasAnyFlagBitsSet(letter))

Implementation:

public static class EnumHelper
{
    static EnumHelper()
    {
        // Required to get correct behavior in GetNumericValue
        // Because we will overlap the enum type with a ulong, left-aligned
        if (!BitConverter.IsLittleEndian)
            throw new NotSupportedException("This type is only supported on little-endian architectures.");
    }

    /// <summary>
    /// <para>
    /// Returns whether the given enum value has any bits set that occurs in a defined flag for <typeparamref name="T"/>.
    /// </para>
    /// <para>
    /// Throws if the type parameter is not an enum type with the <see cref="FlagsAttribute"/>.
    /// </para>
    /// </summary>
    public static bool HasAnyFlagBitsSet<T>(T enumValue)
        where T : unmanaged, Enum
    {
        var numericValue = GetNumericValue(enumValue);

        // Take the value that has all the permitted bits set
        // Use & to keep only the corresponding bits from the input value
        // Check that the input value provided at least one such bit
        return (numericValue & FlagValueCache<T>.AllFlagsSetValue) != 0;
    }

    /// <summary>
    /// <para>
    /// Returns whether the given enum value has any bits set that are set in <paramref name="flags"/>.
    /// </para>
    /// <para>
    /// Throws if the type parameter is not an enum type with the <see cref="FlagsAttribute"/>.
    /// </para>
    /// </summary>
    public static bool HasAnyFlagBitsSet<T>(T enumValue, T flags)
        where T : unmanaged, Enum
    {
        var numericValue = GetNumericValue(enumValue);
        var numericFlags = GetNumericValue(flags);

        // Use & to keep only the bits present in flags
        // Check that the input value provided at least one such bit
        return (numericValue & flags) != 0;
    }

    // Actually, have a bonus method as well, since this is a common operation:

    /// <summary>
    /// <para>
    /// Returns whether the given enum value consists exclusively of defined flags for <typeparamref name="T"/>.
    /// The result is false if a bit is set that is not part of any value defined by <typeparamref name="T"/>.
    /// </para>
    /// <para>
    /// Throws if the type parameter is not an enum type with the <see cref="FlagsAttribute"/>.
    /// </para>
    /// </summary>
    public static bool HasDefinedFlags<T>(T enumValue)
        where T : unmanaged, Enum
    {
        var numericValue = GetNumericValue(enumValue);

        // Take the value that has all the permitted bits set
        // Use ~ to get a value with all the forbidden bits set
        // Use & to keep only the corresponding bits from the input value
        // Check that the input value provided no such forbidden bits
        return (numericValue & ~FlagValueCache<T>.AllFlagsSetValue) == 0;
    }

    /// <summary>
    /// <para>
    /// Returns the numeric value of the given <paramref name="enumValue"/>.
    /// </para>
    /// <para>
    /// The resulting <see cref="ulong"/> can be cast to the intended integral type, even if it is a signed type.
    /// </para>
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ulong GetNumericValue<T>(T enumValue)
        where T : unmanaged, Enum
    {
        Span<ulong> ulongSpan = stackalloc ulong[] { 0UL };
        Span<T> span = MemoryMarshal.Cast<ulong, T>(ulongSpan);

        span[0] = enumValue;

        return ulongSpan[0];
    }

    /// <summary>
    /// Statically caches a "full" flags value each enum type for which this class is accessed.
    /// </summary>
    internal static class FlagValueCache<T>
        where T : unmanaged, Enum
    {
        /// <summary>
        /// Each bit that is set in any of the type's defined values is also set in this value.
        /// </summary>
        public static ulong AllFlagsSetValue { get; }

        static FlagValueCache()
        {
            if (typeof(T).BaseType != typeof(Enum)) throw new Exception("The type parameter must be an enum type.");

            foreach (var value in (T[])Enum.GetValues(typeof(T)))
                AllFlagsSetValue |= GetNumericValue(value);
        }
    }
}

What does it mean that we are checking if at least one flag bit is set?

Well, this solution may fail to answer correctly for nonsensical enums like the following:

[Flags]
public enum Nonsense
{
    One = 1,

    // Eh, why does this value need TWO bits when those bits are NOT defined as individual values?
    TwoAndFour = 2 | 4,
}

Here, EnumHelper.HasAnyFlagBitSet((Nonsense)2) would return true, which is technically incorrect, since 2 is not a defined flag.

However, it works perfectly fine for all sensible flags enums, including ones with multi-flags:

[Flags]
public enum Fine
{
    One = 1,
    Two = 2,
    Four = 4,

    // Fine, and sensible, since these flags exist individually
    TwoAndFour = 2 | 4,
}
痴骨ら 2024-08-10 12:59:09

您可以直接对组合标志常量使用按位与运算符 (&),并检查结果是否不等于 0。

下面是一个使用 Letters 枚举中的 AB 标志的示例:

if ((letter & Letters.AB) != 0)
{
    // AB flag is set
}

这是有效的,因为如果 Letters 中至少有一个标志,则按位 AND 运算只会产生非零结果。 AB 设置在 letter 变量中。如果结果为零,则表示 Letters 中没有任何标志。 AB 以字母形式设置。

您可以应用相同的方法来检查任何其他组合标志常量,例如 All:

if ((letter & Letters.All) != 0)
{
    // At least one flag from All is set
}

通过以这种方式使用按位 AND 运算符,您可以简化检查并避免与标志常量本身进行显式比较。

You can use the bitwise AND operator (&) directly on the combined flag constant and check if the result is not equal to zero.

Here's an example using the AB flag from your Letters enum:

if ((letter & Letters.AB) != 0)
{
    // AB flag is set
}

This works because the bitwise AND operation will only produce a non-zero result if at least one of the flags in Letters. AB is set in the letter variable. If the result is zero, it means none of the flags in Letters. AB are set in letter.

You can apply the same approach to check for any other combined flag constant, such as All:

if ((letter & Letters.All) != 0)
{
    // At least one flag from All is set
}

By using the bitwise AND operator in this way, you can simplify the check and avoid explicitly comparing against the flag constant itself.

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