【特 性】Attribute

1 AttributeUsage

    [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    class MyNewAttribute : System.Attribute
    {
        //
    }

2 Flags

    class Program
    {
        static void Main(string[] args)
        {
            Animal animals = Animal.Dog | Animal.Cat;
            Console.WriteLine(animals.ToString());
        }
    }
    [Flags]
    enum Animal
    {
        Dog = 0x0001,
        Cat = 0x0002,
        Duck = 0x0004,
        Chicken = 0x0008
    }

3 DllImport

        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern int MessageBox(int hParent, string msg, string caption, int type);
        static void Main(string[] args)
        {
            MessageBox(0, "How to use attribute in .NET", "Anytao_net", 0);
        }

4 Serializable

  Serializable 特性表明了应用的元素可以被序列化(serializated),序列化和反序列化是另一个 可以深入讨论的话题,在此我们只是提出概念,深入的研究有待以专门的主题来呈现,限于篇幅, 此不赘述。

5 Conditional

  Conditional 特性,用于条件编译,在调试时使用。注意:Conditional 不可应用于数据成员和 属性。

还有其他的重要特性,包括:Description、DefaultValue、Category、ReadOnly、Brow erAble 等,有时间可以深入研究。

猜你喜欢

转载自www.cnblogs.com/kikyoqiang/p/10331861.html