zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    原创:搜索算法之两个数组取交集的算法
    原创:中文分词的逆向最大匹配算法
    搜索推荐系统根据用户搜索频率(热搜)排序
    原创:Solr Wiki 中关于Suggester(搜索推荐)的简单解读
    从海量文本中统计出前k个频率最高的词语
    原创:从海量数据中查找出前k个最小或最大值的算法(java)
    NOIWC2019 懵逼记
    BZOJ 4568: [Scoi2016]幸运数字(倍增+线性基)
    BZOJ 3207: 花神的嘲讽计划Ⅰ(莫队+哈希)
    BZOJ 3653: 谈笑风生(主席树)
  • 原文地址:https://www.cnblogs.com/chucklu/p/4676123.html
Copyright © 2011-2022 走看看