C# 将标志枚举类型变量中的设置标志转换为整数数组
我想出了这段代码,它将设置的标志转换为标志枚举类型的变量,并将设置的标志作为整数返回。 我想知道这是否是最好的方法。
枚举示例:
[Flags]
enum Status {
None = 0x0,
Active = 0x1,
Inactive = 0x2,
Canceled = 0x4,
Suspended = 0x8
}
我想出的将设置标志转换为 int 数组的扩展方法:
public static class Extensions
{
public static int[] ToIntArray(this System.Enum o)
{
return o.ToString()
.Split(new string[] { ", " }, StringSplitOptions.None)
.Select(i => (int)Enum.Parse(o.GetType(), i))
.ToArray();
}
}
这就是我使用它的方式:
Status filterStatus = Status.Suspended | Status.Canceled;
int[] filterFlags = filterStatus.toIntArray();
foreach (int flag in filterFlags) {
Console.WriteLine("{0}\n", flag);
}
它将输出:
4
8
如您所见,为了完成此操作,我正在执行以下操作:
- 将变量转换为字符串。它输出类似: Suspending, Canceled
- 将该字符串拆分为字符串数组: { "Suspending", "Canceled" }
- 使用 Enum.Parse 将该字符串转换为枚举值。
- 将值转换为整数。
- 将 IEnumerable 转换为 int[]。
它有效,但我认为这不是最好的方法。有什么建议可以改进这段代码吗?
I came up with this piece of code that converts the set flags in a variable of type Flag Enumeration and returns the set flags as integers.
I'd like to know if this is the best approach.
Example enumeration:
[Flags]
enum Status {
None = 0x0,
Active = 0x1,
Inactive = 0x2,
Canceled = 0x4,
Suspended = 0x8
}
The extension method that converts the set flags to array of int that I came up with:
public static class Extensions
{
public static int[] ToIntArray(this System.Enum o)
{
return o.ToString()
.Split(new string[] { ", " }, StringSplitOptions.None)
.Select(i => (int)Enum.Parse(o.GetType(), i))
.ToArray();
}
}
This is how I use it:
Status filterStatus = Status.Suspended | Status.Canceled;
int[] filterFlags = filterStatus.toIntArray();
foreach (int flag in filterFlags) {
Console.WriteLine("{0}\n", flag);
}
It will output:
4
8
As you can see, to get this done I'm doing the following:
- Converting the variable to string. It outputs something like: Suspended, Canceled
- Splitting that string into an array of strings: { "Suspended", "Canceled" }
- Converting that string to the enumeration value with Enum.Parse.
- Casting the value to an integer.
- Converting the IEnumerable to int[].
It works, but I just don't think it's the best approach. Any suggestions to improve this bit of code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保持类似于 linq
这种方法的一个问题是它将包含聚合枚举值。例如:
这里
flags
将有1, 2, 3
,而不仅仅是1, 2
。To keep it linq-like
One gotcha with this approach is that it will include aggregate enumeration values. For example:
Here
flags
will have1, 2, 3
, not just1, 2
.只需转换为 int,然后打印出各个位。
Just cast to an int, and then print out individual bits.
那么,您可以将步骤 1-3 替换为调用 <代码>Enum.GetValues。然后使用按位
&
运算符来测试您的值中设置了哪些。这比使用字符串要快得多。此方法将支持超过一位宽的标志。如果所有位都是独立的,则只需 按照 Anon 说的去做。
Well, you can replace steps 1-3 with a call to
Enum.GetValues
. And then use the bitwise&
operator to test which of those are set in your value. This will be much faster than working with strings.This method will support flags which are more than a single bit wide. If all bits are independent, just do what Anon said.