就是,我看其他人的代码,发现有时候他们在函数前用[]写一行东西,不知道是标记还是什么东西?
请问这是什么?一种语法么?作用是什么?
拜托详细些...谢谢了

解决方案 »

  1.   

    Attribute
    作用是为相应的元素,比如类,方法,接口等提供元信息。
    具体的使用者可以是编译器或者其它任何人。
      

  2.   

    Attribute是可以自行扩展的,你写的类如果继承自:Attribute,那么你就可以在一个类的前面加上相应的标记。
      

  3.   

    加入某些属性后,会得到.NET提供一些底层支持
    比如
    [Seriliable]
    class AAAA
    :这个将告诉编译器这个类AAAA是可以序列化的,那么.net会执行序列化的操作,当你通过WebServices提供AAAA的时候,可以不必自定义实现序列化。
      

  4.   

    找了大半天才找到,
    看以前的这个帖子。我在里面有比较详细的说明了,还有些例子。
    http://topic.csdn.net/u/20080925/10/6bd91e24-69ca-4fda-ac50-b343a550838d.html正是因为你大部分时候不需要说明,所以你一般不用attribute。
      

  5.   

    将 System.FlagsAttribute 应用于某个枚举时,如果该枚举包含一些使用按位“或”运算组合的元素,这时您会注意到该属性在用于某些工具时会影响 enum 的行为。当使用诸如 Console 类方法、表达式计算器这样的工具时,可以注意到这些变化。下面的代码示例阐释 enum 声明上的 System.FlagsAttribute 属性的使用和效果。// enumFlags.cs
    // 在枚举上应用 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
    */
      

  6.   

    下面的代码示例阐释了如何使用 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
    */
      

  7.   

    元信息是关于代码的描述。给类、方法等代码添加元信息,就是描述他们在某一个方面的性质(Attribute),以供程序员或者其它程序使用。
    程序员可以查看这些信息,通过反射机制,程序也可以可看另一个程序的元信息,从而根据它是否具备某一性质而进行工作。比如:[obsolete]就描述这一个类或者方法已经不再建议使用。
    如果你要使用这个类,你在引用这个程序集并看到这个类的时候就知道它有一个[obsolete]的Attribute,这时你就需要慎重考虑是否继续使用这个类,因为加上了这个标签意味着将来可能不再支持这个类或者类中的方法。对于编译器来说,如果发现你使用了一个带有[obsolete]Attribute的类,你在编译程序时会有警告产生。这就是编译器通过反射取得了这个代码的Attribute才得知的。
      

  8.   

    上面大虾的代码也是Attribute的一个很常见的应用。
      

  9.   

    慢慢来,首先你了解Attribute的工作机制,然后你可以在学习和工作中一个个的接触到他们平时也可以看。
    一些常用的Attribute你很快会接触到:
    Serializable
    StructLayout
    MarshalAs
    还有一些和特定的技术相关的,比如:COM,XML,SOAP就有一大堆的相关Attribute,这结好多我都没用过,也不清楚是什么意思。
    学习的有些阶段,要学会不求甚解。如果一味求大求全,也不是好的。技术上的细节有时候还是留给手册去吧……不然MSDN要了做什么。