下面的代码示例阐释了如何使用 FlagsAttribute 属性,并显示了使用 Enum 声明的 FlagsAttribute 在 ToString 方法上的效果。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 - 8All 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 */
作用是为相应的元素,比如类,方法,接口等提供元信息。
具体的使用者可以是编译器或者其它任何人。
比如
[Seriliable]
class AAAA
:这个将告诉编译器这个类AAAA是可以序列化的,那么.net会执行序列化的操作,当你通过WebServices提供AAAA的时候,可以不必自定义实现序列化。
看以前的这个帖子。我在里面有比较详细的说明了,还有些例子。
http://topic.csdn.net/u/20080925/10/6bd91e24-69ca-4fda-ac50-b343a550838d.html正是因为你大部分时候不需要说明,所以你一般不用attribute。
// 在枚举上应用 FlagsAttributeusing System;[Flags]
public enum CarOptions
{
SunRoof = 0x01,
Spoiler = 0x02,
FogLights = 0x04,
TintedWindows = 0x08,
}class FlagTest
{
static void Main()
{
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
Console.WriteLine(options);
Console.WriteLine((int)options);
}
}/* 程序输出:
SunRoof, FogLights
5
*/
{
// 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 - 8All 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
*/
程序员可以查看这些信息,通过反射机制,程序也可以可看另一个程序的元信息,从而根据它是否具备某一性质而进行工作。比如:[obsolete]就描述这一个类或者方法已经不再建议使用。
如果你要使用这个类,你在引用这个程序集并看到这个类的时候就知道它有一个[obsolete]的Attribute,这时你就需要慎重考虑是否继续使用这个类,因为加上了这个标签意味着将来可能不再支持这个类或者类中的方法。对于编译器来说,如果发现你使用了一个带有[obsolete]Attribute的类,你在编译程序时会有警告产生。这就是编译器通过反射取得了这个代码的Attribute才得知的。
一些常用的Attribute你很快会接触到:
Serializable
StructLayout
MarshalAs
还有一些和特定的技术相关的,比如:COM,XML,SOAP就有一大堆的相关Attribute,这结好多我都没用过,也不清楚是什么意思。
学习的有些阶段,要学会不求甚解。如果一味求大求全,也不是好的。技术上的细节有时候还是留给手册去吧……不然MSDN要了做什么。