FlagsAttribute クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<SerializableAttribute> _ <AttributeUsageAttribute(AttributeTargets.Enum, Inherited:=False)> _ <ComVisibleAttribute(True)> _ Public Class FlagsAttribute Inherits Attribute
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Enum, Inherited=false)] [ComVisibleAttribute(true)] public class FlagsAttribute : Attribute
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets::Enum, Inherited=false)] [ComVisibleAttribute(true)] public ref class FlagsAttribute : public Attribute
通常、ビット フィールドは、組み合わせて使用できる要素のリストとして使用されます。一方、列挙定数は、同時には選択できない要素のリストとして使用されます。つまり、ビット フィールドは、ビットごとの OR 演算で組み合わせて無名の値を作成できるような設計になっていますが、列挙定数はそのようには設計されていません。言語によって、列挙定数とビット フィールドの使用方法の違いはさまざまです。
FlagsAttribute の属性FlagsAttribute および Enum のガイドライン
-
ビットごとの演算 (AND、OR、EXCLUSIVE OR) が数値で実行される場合のみ、列挙体の FlagsAttribute カスタム属性を使用します。
-
列挙定数を 2 の累乗 (1、2、4、8 など) で定義してください。これは、結合された列挙定数の各フラグが重複しないことを意味します。
-
よく使われるフラグの組み合わせに対して列挙定数を作成することを検討してください。たとえば、列挙定数の Read = 1 および Write = 2 を含むファイル I/O 操作に使用される列挙体を持っている場合は、Read フラグと Write フラグを結合する列挙定数 ReadWrite = Read OR Write を作成することが考えられます。場合によっては、フラグを結合するために使用されるビットごとの OR 演算は、単純なタスクには必要のない高度な概念と見なされます。
-
フラグ列挙定数として負数を定義する場合は、多数のフラグの位置が 1 に設定される場合があるので注意してください。その結果、コードが混乱してコーディング エラーの原因となる可能性があります。
-
フラグが数値で設定されているかどうかをテストする便利な方法は、数値とフラグ列挙定数の間でビットごとの AND 演算を実行することです。これにより、フラグに対応しない数値内のすべてのビットが 0 に設定されます。次に、その演算の結果がフラグ列挙定数に等しいかどうかをテストします。
-
値が 0 であるフラグ列挙定数の名前として None を使用します。フラグをテストするためにビットごとの AND 演算を実行する場合、None 列挙定数は結果が常に 0 になるので使用できません。ただし、数値と None 列挙定数の間で、ビットごとではなく論理的な比較を実行して、数値のビットが設定されているかどうかを判断できます。
フラグ列挙体の代わりに値の列挙体を作成する場合も、None 列挙定数の作成は有用です。列挙体に使用されるメモリが共通言語ランタイムによって既定で 0 に初期化されるからです。その結果、値が 0 である定数を定義しなかった場合、列挙体には作成時に無効な値が含まれます。
アプリケーションで、既定の状態を表す必要がある場合は、値が 0 である列挙定数を使用してその既定の状態を表すことを検討してください。既定の状態がない場合は、他の列挙定数では表現されない状態を表すために、値が 0 の列挙定数の使用を検討してください。
-
列挙体そのものの状態を反映するだけの列挙値を定義しないでください。たとえば、列挙体の末尾をマークするだけの列挙定数は定義しないでください。列挙体の末尾の値を確認する必要がある場合は、その値を明示的にチェックしてください。また、範囲内のすべての値が有効である場合は、先頭と末尾の列挙定数を確認する範囲のチェックを実行できます。
-
列挙定数を値として受け取るメソッドまたはプロパティを定義する場合は、値を検証することを検討してください。数値が列挙体に定義されていない場合も、数値を列挙型にキャストできるからです。
FlagsAttribute 属性を Enum 宣言で使用した場合の ToString メソッドへの影響を示すコード例を次に示します。
' Example of the FlagsAttribute attribute. Imports System Imports Microsoft.VisualBasic Module FlagsAttributeDemo ' Define an Enum without FlagsAttribute. Enum SingleHue as Short Black = 0 Red = 1 Green = 2 Blue = 4 End Enum ' Define an Enum with FlagsAttribute. <FlagsAttribute( )> _ Enum MultiHue as Short Black = 0 Red = 1 Green = 2 Blue = 4 End Enum Sub Main( ) Console.WriteLine( _ "This example of the FlagsAttribute attribute " & _ vbCrLf & "generates the following output." ) Console.WriteLine( vbCrLf & _ "All possible combinations of values of an " & _ vbCrLf & "Enum without FlagsAttribute:" & vbCrLf ) ' Display all possible combinations of values. Dim val as Integer For val = 0 to 8 Console.WriteLine( "{0,3} - {1}", _ val, CType( val, SingleHue ).ToString( ) ) Next val Console.WriteLine( vbCrLf & _ "All possible combinations of values of an " & _ vbCrLf & "Enum with FlagsAttribute:" & vbCrLf ) ' Display all possible combinations of values. ' Also display an invalid value. For val = 0 to 8 Console.WriteLine( "{0,3} - {1}", _ val, CType( val, MultiHue ).ToString( ) ) Next val End Sub End Module ' 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
// 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 */
// Example of the FlagsAttribute attribute. using namespace System; // Define an Enum without FlagsAttribute. enum class SingleHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; // Define an Enum with FlagsAttribute. [FlagsAttribute] enum class MultiHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; int 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 */
System.Attribute
System.FlagsAttribute
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- FlagsAttribute クラスのページへのリンク