http://blog.sina.com.cn/s/blog_429fe72e010006u3.html
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Enum, Inherited = false)] [ComVisibleAttribute(true)] public class FlagsAttribute : Attribute
位域一般用於由可組合出現的元素組成的列表,而枚舉常數一般用於由互相排斥的元素組成的列表。所以,位域設計爲經過按位「或」運算組合來生成未命名的值,而枚舉常數則不是。語言在對位域的使用和對枚舉常數的使用上不一樣。html
AttributeUsageAttribute 應用於此類,其 Inherited 屬性指定 false。此屬性只能應用於枚舉。安全
只有要對數值執行按位運算(AND、OR、XOR)時纔對枚舉使用 FlagsAttribute 自定義屬性。測試
用 2 的冪(即 一、二、四、8 等)定義枚舉常量。這意味着組合的枚舉常量中的各個標誌都不重疊。spa
請考慮爲經常使用標誌組合建立一個枚舉常量。例如,若是用於文件 I/O 操做的枚舉包含枚舉常量 Read = 1和 Write = 2,請考慮建立枚舉常量 ReadWrite = Read OR Write,該常量組合了 Read 和 Write 標誌。此外,在某些狀況下,可能會將用於組合標誌的按位 OR 運算視爲一種高級概念,在簡單任務中不須要執行此操做。線程
將負數定義爲標誌枚舉常量時應謹慎,由於不少標誌位置均可能設置爲 1,這可能使您的代碼產生混淆並易於發生代碼錯誤。設計
測試數值中是否已設置標誌的一種簡便方法爲:在數值和標誌枚舉常量之間執行按位「與」操做,這種方法會將數值中與標誌不對應的全部位都設置爲零,而後測試該操做的結果是否等於該標誌枚舉常量。htm
將 None 用做值爲零的標誌枚舉常量的名稱。在按位 AND 運算中,不能使用 None 枚舉常量測試標誌,由於所得的結果始終爲零。可是,您能夠在數值與 None 枚舉常量之間執行邏輯(不是按位)比較,以肯定數值中是否已設置任何位。blog
若是建立的是值枚舉而不是標誌枚舉,建立 None 枚舉常量仍十分有用。緣由是在默認狀況下,公共語言運行庫會將用於枚舉的內存初始化爲零。所以,若是不定義值爲零的常量,則枚舉在建立時將包含非法值。繼承
若是明顯存在應用程序須要表示的默認狀況,請考慮使用值爲零的枚舉常量表示默認值。若是不存在默認狀況,請考慮使用值爲零的枚舉常量(這意味着該狀況不禁任何其餘枚舉常量表示)。內存
不要僅爲了反映枚舉自身的狀態而定義枚舉值。例如,不要定義僅用於標記枚舉末尾的枚舉常量。若是須要肯定枚舉的最後一個值,請顯式檢查該值。此外,若是枚舉常量範圍中的全部值都有效,還能夠對第一個和最後一個枚舉常量執行範圍檢查。
不要指定保留供未來使用的枚舉常量。
在定義採用枚舉常量做爲值的方法或屬性時,應考慮對該值進行驗證。緣由是即便沒有在枚舉中定義某個數值,也能夠將該數值強制轉換爲枚舉類型。
下面的代碼示例闡釋瞭如何使用 FlagsAttribute 屬性,並顯示了使用 Enum 聲明的 FlagsAttribute 在ToString 方法上的效果。
// Example of the FlagsAttribute attribute.
using System;
class FlagsAttributeDemo
{
// Define an Enum without FlagsAttribute.
enum SingleHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};
// Define an Enum with FlagsAttribute.
[FlagsAttribute]
enum MultiHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};
static void Main( )
{
Console.WriteLine(
"This example of the FlagsAttribute attribute \n" +
"generates the following output." );
Console.WriteLine(
"\nAll possible combinations of values of an \n" +
"Enum without FlagsAttribute:\n" );
// Display all possible combinations of values.
for( int val = 0; val <= 8; val++ )
Console.WriteLine( "{0,3} - {1}",
val, ( (SingleHue)val ).ToString( ) );
Console.WriteLine(
"\nAll possible combinations of values of an \n" +
"Enum with FlagsAttribute:\n" );
// Display all possible combinations of values.
// Also display an invalid value.
for( int val = 0; val <= 8; val++ )
Console.WriteLine( "{0,3} - {1}",
val, ( (MultiHue)val ).ToString( ) );
}
}
/*
This example of the FlagsAttribute attribute
generates the following output.
All possible combinations of values of an
Enum without FlagsAttribute:
0 - Black
1 - Red
2 - Green
3 - 3
4 - Blue
5 - 5
6 - 6
7 - 7
8 - 8
All possible combinations of values of an
Enum with FlagsAttribute:
0 - Black
1 - Red
2 - Green
3 - Red, Green
4 - Blue
5 - Red, Blue
6 - Green, Blue
7 - Red, Green, Blue
8 - 8
*/
Windows 9八、Windows 2000 SP四、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 200三、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP二、Windows XP Starter Edition
.NET Framework 並非對每一個平臺的全部版本都提供支持。有關受支持版本的列表,請參見系統要求。