.net FlagsAttribute 枚举是否需要手动赋值?

发布于 2024-08-09 09:45:29 字数 892 浏览 3 评论 0原文

为了允许在显示新闻故事的方法上使用不同的格式选项,我创建了一个可以传入的枚举来指定它的显示方式。

[Flags]
private enum NewsStyle
{
    Thumbnail = 0,
    Date = 1,
    Text = 2,
    Link = 4,
    All = 8
}

string FormatNews( DataRow news, NewsStyle style )
{
    StringBuilder HTML = new StringBuilder();

    // Should the link be shown
    if ( ((newsStyle & NewsStyle.All) == NewsStyle.All || (newsStyle & NewsStyle.Link) == NewsStyle.Link))
    {
                HTML.AppendFormat("<a style=\"text-decoration:none; color: rgb(66, 110, 171);\" href=\"ViewStory.aspx?nsid={0}\">",
                                  UrlEncode(newsStory["NewsStoryID"].ToString()));
    }

    // Etc etc...
}

// So to call the method...
Response.Write( FormatNews( news, NewsStyle.Date | NewsStyle.Text ) );

问题是,只有在手动指定枚举上的值时,我才能使代码正常工作,否则按位枚举检查操作将无法正常工作。

我一直遵循让 .net 处理枚举值分配的规则 - 这是一个真正的例外吗?

To allow for different formatting options on a method that displays a news story, I've created an enumeration that can be passed in to specify how it gets displayed.

[Flags]
private enum NewsStyle
{
    Thumbnail = 0,
    Date = 1,
    Text = 2,
    Link = 4,
    All = 8
}

string FormatNews( DataRow news, NewsStyle style )
{
    StringBuilder HTML = new StringBuilder();

    // Should the link be shown
    if ( ((newsStyle & NewsStyle.All) == NewsStyle.All || (newsStyle & NewsStyle.Link) == NewsStyle.Link))
    {
                HTML.AppendFormat("<a style=\"text-decoration:none; color: rgb(66, 110, 171);\" href=\"ViewStory.aspx?nsid={0}\">",
                                  UrlEncode(newsStory["NewsStoryID"].ToString()));
    }

    // Etc etc...
}

// So to call the method...
Response.Write( FormatNews( news, NewsStyle.Date | NewsStyle.Text ) );

The problem is that I can only get the code to work if I manually specify the values on the enum, otherwise the bitwise enum checking operation doesn't work correctly.

I've always followed the rule of let .net handle the assigning of values to enums - is this a geniuine exception?

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

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

发布评论

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

评论(2

听风吹 2024-08-16 09:45:30

是的,您必须在枚举中指定值才能使添加的标志值正常工作(以便能够组合两个枚举标志并且不会使该值与指定的另一个标志冲突)。您不必指定值来编译它,但结果很可能不是您正在寻找的结果。

Yes, you have to specify the values in the Enum to get the added flag values to work correctly (to be able to combine two enum flags and not have that value conflict with another flag specified). You don't have to specify values to get it to compile, but the outcome will most likely not be the one you are looking for.

年华零落成诗 2024-08-16 09:45:29

是的,这是一个真正的例外。默认情况下,枚举从 0 开始分配值,每个值都比前一个值高 1,无论任何 FlagsAttribute 是多少。 (当然,当您手动指定某些值时,还有更详细的规则 - 请参阅 MSDN 文档

虽然让标志枚举自动获取 2 的幂值确实有点有意义,但我明白为什么它可能是这样的最好不要这样做:

拥有这样的枚举是完全合理的:

[Flags]
enum Foo
{
    None,
    Bar,
    Baz, 
    BarAndBaz
} 

这当然要求值是 0、1、2 和 3 才合理。

让一个属性的存在改变一段 C# 代码的全部含义是非常违反直觉的。考虑我的 Foo 示例枚举。是否应该允许删除 Flags 属性来通过静默更改枚举值来破坏代码?

Yes, it is a genuine exception. Enumerations are by default assigned values from 0 onwards, each one 1 higher than the previous value, regardless of any FlagsAttributes. (There of course are more elaborate rules for when you specify some values manually – see the MSDN docs)

While it does somewhat make sense to have a flags enumeration automatically get values in powers of two, I see reasons why it's probably best not to do so:

It's perfectly reasonable to have an enum like this:

[Flags]
enum Foo
{
    None,
    Bar,
    Baz, 
    BarAndBaz
} 

This of course requires the values to be 0, 1, 2 and 3 to be sensible.

It'd be pretty counter-intuitive to let the presence of a mere attribute change the entire meaning of a piece of C# code. Consider my Foo example enumeration. Should removing the Flags attribute be allowed to break the code by silently changing the enumerator values?

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