C#语法手册

特性Attribute使用语法


使用特性,可以用声明的方式将信息与代码相关联。 特性还可以提供能够应用于各种目标的可重用元素。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSGDemoCSG008
{

    //#csg008-01
    //声明特性, 所有特性都需要继承自Attribute
    public class MyAttribute : Attribute
    {
    }


    //#csg008-02
    //限定特性的使用范围,可以用 | 来分割多种限定
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class MyAttributeForClassAndStructOnly : Attribute
    {
    }

    //特性使用范围包括
    //#csg008-02程序集
    [AttributeUsage(AttributeTargets.Assembly)]
    public class MyAttribute1 : Attribute { }
    //#csg008-02类
    [AttributeUsage(AttributeTargets.Class)]
    public class MyAttribute2 : Attribute { }
    //#csg008-02构造函数
    [AttributeUsage(AttributeTargets.Constructor)]
    public class MyAttribute3 : Attribute { }
    //#csg008-02委托
    [AttributeUsage(AttributeTargets.Delegate)]
    public class MyAttribute4 : Attribute { }
    //#csg008-02枚举
    [AttributeUsage(AttributeTargets.Enum)]
    public class MyAttribute5 : Attribute { }
    //#csg008-02事件
    [AttributeUsage(AttributeTargets.Event)]
    public class MyAttribute6 : Attribute { }
    //#csg008-02字段
    [AttributeUsage(AttributeTargets.Field)]
    public class MyAttribute7 : Attribute { }
    //#csg008-02泛型参数
    [AttributeUsage(AttributeTargets.GenericParameter)]
    public class MyAttribute8 : Attribute { }
    //#csg008-02接口
    [AttributeUsage(AttributeTargets.Interface)]
    public class MyAttribute9 : Attribute { }
    //#csg008-02方法
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute10 : Attribute { }
    //#csg008-02模块
    [AttributeUsage(AttributeTargets.Module)]
    public class MyAttribute11 : Attribute { }
    //#csg008-02参数
    [AttributeUsage(AttributeTargets.Parameter)]
    public class MyAttribute12 : Attribute { }
    //#csg008-02属性 properties
    [AttributeUsage(AttributeTargets.Property)]
    public class MyAttribute13 : Attribute { }
    //#csg008-02返回值
    [AttributeUsage(AttributeTargets.ReturnValue)]
    public class MyAttribute14 : Attribute { }
    //#csg008-02结构
    [AttributeUsage(AttributeTargets.Struct)]
    public class MyAttribute15 : Attribute { }


    //#csg008-03
    //特性的使用
    //在类中使用
    [MyAttribute2]
    public class MyClass
    {
        //#csg008-03
        //在构造函数使用
        [MyAttribute3]
        public MyClass()
        {

        }

        //#csg008-03
        //方法上使用特性
        [MyAttribute10]
        //返回类型特性
        [return: MyAttribute14]
        //参数上添加特性
        public void MyFuntion([MyAttribute12] string param)
        {

        }

    }


    //#csg008-04
    //获取特性信息
    [MyAttribute2]
    public class GetAttributeInfo
    {
        public void F()
        {
            var typeinfo = typeof(GetAttributeInfo);
            var attrs = typeinfo.GetCustomAttributes(false);
            foreach (var attr in attrs)
                Console.WriteLine("Attribute on MyClass: " + attr.GetType().Name);
        }
    }



}