如何将 IEnumerable转换为C# 中的枚举?

发布于 2024-11-28 05:15:25 字数 448 浏览 1 评论 0原文

我已将多个字符串解析为枚举标志,但看不到将它们合并为单个枚举位字段的巧妙方法。

我使用的方法循环遍历字符串值,然后 |= 将值转换为 Enum 对象,如下所示:

[Flags]
public enum MyEnum { None = 0, First = 1, Second = 2, Third = 4 }
...

string[] flags = { "First", "Third" };
MyEnum e = MyEnum.None;

foreach (string flag in flags)
    e |= (MyEnum)Enum.Parse(typeof(MyEnum), flag, true);

我尝试使用 Select 方法转换为我的 Enum 类型,但后来我陷入了 IEnumerable。有什么建议吗?

I've parsed several strings into Enum flags but can't see a neat way of merging them into a single Enum bitfield.

The method I'm using loops through the string values then |= the casted values to the Enum object, like so:

[Flags]
public enum MyEnum { None = 0, First = 1, Second = 2, Third = 4 }
...

string[] flags = { "First", "Third" };
MyEnum e = MyEnum.None;

foreach (string flag in flags)
    e |= (MyEnum)Enum.Parse(typeof(MyEnum), flag, true);

I've tried using a Select method to convert to my Enum type, but then I'm stuck with IEnumerable<MyEnum>. Any suggestions?

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

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

发布评论

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

评论(2

七分※倦醒 2024-12-05 05:15:26

好吧,在 IEnumerable 中,您可以使用:

MyEnum result = parsed.Aggregate((current, next) => current | next);

或者为了容纳空序列:

MyEnum result = parsed.Aggregate(MyEnum.None, (current, next) => current | next);

不可否认,它基本上与您已经得到的东西相同......

所以总的来说,代码将是:(

MyEnum result = flags.Select(x => (MyEnum) Enum.Parse(typeof(MyEnum), x))
                     .Aggregate(MyEnum.None, (current, next) => current | next);

您可以根据 Guffa 的回答在单个 Aggregate 调用中执行它,但我个人认为为了清楚起见,我会将两者分开。不过,这是个人偏好。)

请注意,我的 <一个href="http://code.google.com/p/unconstrained-melody/">Unconstrained Melody 项目使枚举处理更加愉快,您还可以使用通用的 .NET 4 中的Enum.TryParse 方法。

例如,使用 Unconstrained Melody 您可以使用:

MyEnum result = flags.Select(x => Enums.ParseName<MyEnum>(x))
                     .Aggregate(MyEnum.None, (current, next) => current | next);

Well, from an IEnumerable<MyEnum> you can use:

MyEnum result = parsed.Aggregate((current, next) => current | next);

or in order to accommodate an empty sequence:

MyEnum result = parsed.Aggregate(MyEnum.None, (current, next) => current | next);

It's basically the same thing as you've already got, admittedly...

So overall, the code would be:

MyEnum result = flags.Select(x => (MyEnum) Enum.Parse(typeof(MyEnum), x))
                     .Aggregate(MyEnum.None, (current, next) => current | next);

(You can perform it in a single Aggregate call as per Guffa's answer, but personally I think I'd keep the two separate, for clarity. It's a personal preference though.)

Note that my Unconstrained Melody project makes enum handling somewhat more pleasant, and you can also use the generic Enum.TryParse method in .NET 4.

So for example, using Unconstrained Melody you could use:

MyEnum result = flags.Select(x => Enums.ParseName<MyEnum>(x))
                     .Aggregate(MyEnum.None, (current, next) => current | next);
悲喜皆因你 2024-12-05 05:15:26

您可以使用 Aggregate 方法将标志放在一起:

MyEnum e = flags
  .Select(s => (MyEnum)Enum.Parse(typeof(MyEnum), s, true))
  .Aggregate(MyEnum.None, (f, n) => f | n);

You can use the Aggregate method to put the flags together:

MyEnum e = flags
  .Select(s => (MyEnum)Enum.Parse(typeof(MyEnum), s, true))
  .Aggregate(MyEnum.None, (f, n) => f | n);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文