Attribute

Attribute

 

The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.


Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.


All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied.


The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes.

 

复制代码
namespace AttributeDemo
{
    public enum Animal
    {
        Dog = 1,
        Cat,
        Bird
    }
}
复制代码
复制代码
using System;

namespace AttributeDemo
{
    class AnimalTypeAttribute : Attribute
    {
        public AnimalTypeAttribute(Animal animal)
        {
            pet = animal;
        }

        protected Animal pet;
        public Animal Pet
        {
            get { return pet; }
            set { pet = value; }
        }
    }
}
复制代码
复制代码
using System;

namespace AttributeDemo
{
    class AnimalTypeTest
    {
        [AnimalType(Animal.Dog)]
        public void DogMethod()
        { }

        [AnimalType(Animal.Cat)]
        public void CatMethod()
        { }

        [AnimalType(Animal.Bird)]
        public void BirdMethod()
        { }
    }
}
复制代码
复制代码
using System;
using System.Reflection;

namespace AttributeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            AnimalTypeTest animalTypeTest = new AnimalTypeTest();
            Type type = animalTypeTest.GetType();

            MethodInfo[] methodInfoArray = type.GetMethods();
            foreach (MethodInfo item in methodInfoArray)
            {
                Attribute[] attributeArray = Attribute.GetCustomAttributes(item);
                foreach (Attribute item1 in attributeArray)
                {
                    AnimalTypeAttribute animalTypeAttribute = item1 as AnimalTypeAttribute;
                    if (animalTypeAttribute != null)
                    {
                        Console.WriteLine(string.Format("{0} {1}", item.Name, animalTypeAttribute.Pet));
                    }
                }
            }
            Console.ReadLine();
        }
    }
}
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(274)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示