【发布时间】:2009-10-22 14:02:03
【问题描述】:
为了在显示新闻报道的方法上允许使用不同的格式选项,我创建了一个可以传入的枚举,以指定它的显示方式。
[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 处理向枚举分配值的规则 - 这是一个真正的例外吗?
【问题讨论】:
标签: c# .net enums bit-manipulation