理解AttributeUsage类

类定义:

    // 摘要: 
    //     指定另一特性类的用法。 此类不能被继承。
    [Serializable]
    [AttributeUsage(AttributeTargets.Class, Inherited = true)]
    [ComVisible(true)]
    public sealed class AttributeUsageAttribute : Attribute
    {
        // 摘要: 
        //     用指定的 System.AttributeTargets、System.AttributeUsageAttribute.AllowMultiple
        //     值和 System.AttributeUsageAttribute.Inherited 值列表初始化 System.AttributeUsageAttribute
        //     类的新实例。
        //
        // 参数: 
        //   validOn:
        //     使用按位"或"运算符组合的一组值,用于指示哪些程序元素是有效的。
        public AttributeUsageAttribute(AttributeTargets validOn);

        // 摘要: 
        //     获取或设置一个布尔值,该值指示能否为一个程序元素指定多个指示属性实例。
        //
        // 返回结果: 
        //     如果允许指定多个实例,则为 true;否则为 false。 默认值为 false。
        public bool AllowMultiple { get; set; }
        //
        // 摘要: 
        //     获取或设置一个布尔值,该值指示指示的属性能否由派生类和重写成员继承。
        //
        // 返回结果: 
        //     如果该属性可由派生类和重写成员继承,则为 true,否则为 false。 默认值为 true。
        public bool Inherited { get; set; }
        //
        // 摘要: 
        //     获取一组值,这组值标识指示的属性可应用到的程序元素。
        //
        // 返回结果: 
        //     一个或多个 System.AttributeTargets 值。 默认值为 All。
        public AttributeTargets ValidOn { get; }
    }
View Code

从AttributeUsage特性的名称上就可以看出它用于描述特性的使用方式。具体来说,首先应该是其所标记的特性可以应用于哪些类型或者对象。AttributeUsage特性的构造函数接受一个 AttributeTargets 类型的参数。

AttributeTargets 是一个位标记,它定义了特性可以应用的类型和对象:

    // 摘要: 
    //     指定可以对它们应用特性的应用程序元素。
    [Serializable]
    [ComVisible(true)]
    [Flags]
    public enum AttributeTargets
    {
        // 摘要: 
        //     可以对程序集应用属性。
        Assembly = 1,
        //
        // 摘要: 
        //     可以对模块应用属性。
        Module = 2,
        //
        // 摘要: 
        //     可以对类应用属性。
        Class = 4,
        //
        // 摘要: 
        //     可以对结构应用属性,即值类型。
        Struct = 8,
        //
        // 摘要: 
        //     可以对枚举应用属性。
        Enum = 16,
        //
        // 摘要: 
        //     可以对构造函数应用属性。
        Constructor = 32,
        //
        // 摘要: 
        //     可以对方法应用属性。
        Method = 64,
        //
        // 摘要: 
        //     可以对属性 (Property) 应用属性 (Attribute)。
        Property = 128,
        //
        // 摘要: 
        //     可以对字段应用属性。
        Field = 256,
        //
        // 摘要: 
        //     可以对事件应用属性。
        Event = 512,
        //
        // 摘要: 
        //     可以对接口应用属性。
        Interface = 1024,
        //
        // 摘要: 
        //     可以对参数应用属性。
        Parameter = 2048,
        //
        // 摘要: 
        //     可以对委托应用属性。
        Delegate = 4096,
        //
        // 摘要: 
        //     可以对返回值应用属性。
        ReturnValue = 8192,
        //
        // 摘要: 
        //     可以对泛型参数应用属性。
        GenericParameter = 16384,
        //
        // 摘要: 
        //     可以对任何应用程序元素应用属性。
        All = 32767,
    }
View Code

AllowMutiple 属性用于设置该特性是不是可以重复地添加到一个类型上(默认为false)。

 Inherited 就更复杂一些了,假如有一个类继承自我们的DemoClass,那么当我们将RecordAttribute添加到DemoClass上时,DemoClass的子类也会获得该特性。而当特性应用于一个方法,如果继承自该类的子类将这个方法覆盖,那么Inherited则用于说明是否子类方法是否继承这个特性。

c#自定义Attribute例子:

    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, Inherited=false, AllowMultiple=true)]
    public class RecordAttribute : Attribute
    {
        public string RecordType { get; private set; }
        public string Author { get; private set; }
        public DateTime Date { get; private set; }
        public string Memo { get; set; }

        public RecordAttribute(string recordType, string author, string date)
        {
            this.RecordType = recordType;
            this.Author = author;
            this.Date = Convert.ToDateTime(date);
        }
    }
View Code

使用该特性:

    [Record("更新", "zwk", "2015-01-01",Memo="优化")]
    [Record("更新", "zwk", "2015-01-02", Memo = "优化")]
View Code

参考:http://www.csharpwin.com/csharpspace/7700r2735.shtml

原文地址:https://www.cnblogs.com/zhengwk/p/5288278.html