将枚举值数组转换为位标志组合

发布于 2024-12-07 09:09:59 字数 510 浏览 1 评论 0原文

如何在 C# 2.0 中以最简单、最优的方式从枚举值数组创建位标志组合。我实际上已经找到了解决方案,但我只是对这里的复杂性不满意。

enum MyEnum
{
    Apple = 0,
    Apricot = 1,
    Breadfruit = 2,
    Banana = 4
}

private int ConvertToBitFlags(MyEnum[] flags)
{
    string strFlags = string.Empty;
    foreach (MyEnum f in flags)
    {
        strFlags += strFlags == string.Empty ?
            Enum.GetName(typeof(MyEnum), f) :
            "," + Enum.GetName(typeof(MyEnum), f);
    }
    return (int)Enum.Parse(typeof(MyEnum), strFlags);
}

How to create a bit-flag combination from an array of enum values in the simplest most optimal way in C# 2.0. I have actually figured out a solution but I am just not satisfied with the complexity here.

enum MyEnum
{
    Apple = 0,
    Apricot = 1,
    Breadfruit = 2,
    Banana = 4
}

private int ConvertToBitFlags(MyEnum[] flags)
{
    string strFlags = string.Empty;
    foreach (MyEnum f in flags)
    {
        strFlags += strFlags == string.Empty ?
            Enum.GetName(typeof(MyEnum), f) :
            "," + Enum.GetName(typeof(MyEnum), f);
    }
    return (int)Enum.Parse(typeof(MyEnum), strFlags);
}

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

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

发布评论

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

评论(2

行雁书 2024-12-14 09:09:59
int result = 0;
foreach (MyEnum f in flags)
{
    result |= f; // You might need to cast — (int)f.
}
return result;

OTOH,您应该使用 FlagsAttribute 来提高类型安全性:

[Flags]
enum MyEnum { ... }

private MyEnum ConvertToBitFlags(MyEnum[] flags)
{
    MyEnum result = 0;
    foreach (MyEnum f in flags)
    {
        result |= f;
    }
    return result;
}

更好的是,通过使用 FlagsAttribute 您可以避免使用 MyEnum[]完全,从而使该方法变得多余。

int result = 0;
foreach (MyEnum f in flags)
{
    result |= f; // You might need to cast — (int)f.
}
return result;

OTOH, you should use the FlagsAttribute for improved type safety:

[Flags]
enum MyEnum { ... }

private MyEnum ConvertToBitFlags(MyEnum[] flags)
{
    MyEnum result = 0;
    foreach (MyEnum f in flags)
    {
        result |= f;
    }
    return result;
}

Better still, by using FlagsAttribute you may be able to avoid using a MyEnum[] entirely, thus making this method redundant.

轮廓§ 2024-12-14 09:09:59

这是一个较短的通用扩展版本:

public static T ConvertToFlag<T>(this IEnumerable<T> flags) where T : struct, IConvertible
{
    if (!typeof(T).IsEnum)
        throw new NotSupportedException($"{typeof(T).ToString()} must be an enumerated type");

    return (T)(object)flags.Cast<int>().Aggregate(0, (c, n) => c |= n);
}

并使用:

[Flags]
public enum TestEnum
{
    None = 0,
    Test1 = 1,
    Test2 = 2,
    Test4 = 4
}

[Test]
public void ConvertToFlagTest()
{
    var testEnumArray = new List<TestEnum> { TestEnum.Test2, TestEnum.Test4 };

    var res = testEnumArray.ConvertToFlag();

    Assert.AreEqual(TestEnum.Test2 | TestEnum.Test4, res);
}

Here's a shorter generic extension version:

public static T ConvertToFlag<T>(this IEnumerable<T> flags) where T : struct, IConvertible
{
    if (!typeof(T).IsEnum)
        throw new NotSupportedException($"{typeof(T).ToString()} must be an enumerated type");

    return (T)(object)flags.Cast<int>().Aggregate(0, (c, n) => c |= n);
}

And using:

[Flags]
public enum TestEnum
{
    None = 0,
    Test1 = 1,
    Test2 = 2,
    Test4 = 4
}

[Test]
public void ConvertToFlagTest()
{
    var testEnumArray = new List<TestEnum> { TestEnum.Test2, TestEnum.Test4 };

    var res = testEnumArray.ConvertToFlag();

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