爲了在顯示新聞報道的方法上允許不同的格式選項,我創建了一個可以傳入的枚舉來指定它的顯示方式。做.net FlagsAttribute枚舉需要手動賦值嗎?
[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));
的問題是,我只能得到,如果我上手工指定枚舉值的代碼工作,否則按位枚舉檢查操作無法正常工作。
我一直遵循let的規則處理賦值給枚舉 - 這是一個geniuine異常嗎?
感謝您的回答和關於推理的優點 – 2009-10-22 14:29:08