Enum.IsDefined 带有标记的枚举

发布于 2024-10-16 19:48:06 字数 488 浏览 7 评论 0原文

我目前正在阅读C# 4.0 in a Nutshell这本书,顺便说一句我认为是一本很棒的书,甚至对于高级程序员来说也可以作为很好的参考。

我回顾了有关基础知识的章节,发现了一个技巧来判断在使用标记枚举时是否在枚举中定义了某个值。
该书指出,使用 Enum.IsDefined 不适用于标记的枚举,并建议采用如下解决方法:

static bool IsFlagDefined(Enum e)
{
    decimal d;
    return (!decimal.TryParse(e.ToString(), out d);
}

如果在标记的枚举中定义了某个值,则应返回 true。

有人可以向我解释为什么这有效吗?

提前致谢 :)

I'm currently reading the book C# 4.0 in a Nutshell, which by the way I think is an excellent book, even for advanced programmers to use as a good reference.

I was looking back on the chapters about the basics, and I came across a trick to tell if a certain value is defined in an Enum when using flagged enums.
The book states that using Enum.IsDefined doesn't work on flagged enums, and suggests a work-around like this :

static bool IsFlagDefined(Enum e)
{
    decimal d;
    return (!decimal.TryParse(e.ToString(), out d);
}

This should return true if a certain value is defined in an enum which is flagged.

Can someone please explain to me why this works ?

Thanks in advance :)

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

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

发布评论

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

评论(2

悟红尘 2024-10-23 19:48:06

基本上,对使用 [Flags] 属性声明的类型的任何 enum 值调用 ToString 将为任何定义的值返回类似以下内容

SomeValue, SomeOtherValue

:另一方面,如果该值enum类型中定义,则ToString将简单地生成该值的字符串表示形式整数值,例如:

5

所以这意味着如果你可以将ToString的输出解析为数字(不确定作者为什么选择decimal),它没有在类型中定义。

这是一个例子:

[Flags]
enum SomeEnum
{
    SomeValue = 1,
    SomeOtherValue = 2,
    SomeFinalValue = 4
}

public class Program
{
    public static void Main()
    {
        // This is defined.
        SomeEnum x = SomeEnum.SomeOtherValue | SomeEnum.SomeFinalValue;

        Console.WriteLine(x);

        // This is not (no bitwise combination of 1, 2, and 4 will produce 8).
        x = (SomeEnum)8;

        Console.WriteLine(x);
    }
}

上述程序的输出是:

SomeOtherValue, SomeFinalValue
8

因此您可以看到建议的方法是如何工作的。

Basically, calling ToString on any enum value of a type declared with the [Flags] attribute will return something like this for any defined value:

SomeValue, SomeOtherValue

On the other hand, if the value is not defined within the enum type, then ToString will simply produce a string representation of that value's integer value, e.g.:

5

So what this means is that if you can parse the output of ToString as a number (not sure why the author chose decimal), it isn't defined within the type.

Here's an illustration:

[Flags]
enum SomeEnum
{
    SomeValue = 1,
    SomeOtherValue = 2,
    SomeFinalValue = 4
}

public class Program
{
    public static void Main()
    {
        // This is defined.
        SomeEnum x = SomeEnum.SomeOtherValue | SomeEnum.SomeFinalValue;

        Console.WriteLine(x);

        // This is not (no bitwise combination of 1, 2, and 4 will produce 8).
        x = (SomeEnum)8;

        Console.WriteLine(x);
    }
}

The output of the above program is:

SomeOtherValue, SomeFinalValue
8

So you can see how the suggested method works.

栖竹 2024-10-23 19:48:06

如果 e 的值不能使用标志组合创建,则 ToString() 默认为整数。整数当然会解析为小数

但是我并不完全清楚为什么你的代码解析为十进制。但整数类型可能不适用于基于 Int64UInt64enum

If the value of e isn't can't be created using a combination of flags ToString() defaults to an integral number. And integral numbers will of course parse as decimal.

But why your code parses as decimal isn't entirely clear to me. But probably integral types won't work for both enums that are based in Int64 and UInt64.

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