zoukankan      html  css  js  c++  java
  • 如何自定义Attribute class并将其应用到相应的class

    摘自于MSND

    Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。

    属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。

    所有属性类型都直接或间接地从 Attribute 类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用 AttributeTargets 类可以指定属性所应用到的目标元素。

    using System;
    using System.Reflection;

    namespace CustomAttrCS {
        // An enumeration of animals. Start at 1 (0 = uninitialized).
        public enum Animal {
            // Pets.
            Dog = 1,
            Cat,
            Bird,
        }

        // A custom attribute to allow a target to have a pet.
        public class AnimalTypeAttribute : Attribute {
            // The constructor is called when the attribute is set.
            public AnimalTypeAttribute(Animal pet) {
                thePet = pet;
            }

            // Keep a variable internally ...
            protected Animal thePet;

            // .. and show a copy to the outside world.
            public Animal Pet {
                get { return thePet; }
                set { thePet = Pet; }
            }
        }

        // A test class where each method has its own pet.
        class AnimalTypeTestClass {
            [AnimalType(Animal.Dog)]
            public void DogMethod() {}

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

            [AnimalType(Animal.Bird)]
            public void BirdMethod() {}
        }

        class DemoClass {
            static void Main(string[] args) {
                AnimalTypeTestClass testClass = new AnimalTypeTestClass(); Type type = testClass.GetType(); // Iterate through all the methods of the class.
                foreach(MethodInfo mInfo in type.GetMethods()) {
                    // Iterate through all the Attributes for each method.
                    foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo)) { // Check for the AnimalType attribute.
                        if (attr.GetType() == typeof(AnimalTypeAttribute))
                            Console.WriteLine(
                                "Method {0} has a pet {1} attribute.",
                                mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
                    }

                }
            }
        }
    }

    /*
    * Output:
    * Method DogMethod has a pet Dog attribute.
    * Method CatMethod has a pet Cat attribute.
    * Method BirdMethod has a pet Bird attribute.
    */

  • 相关阅读:
    Unity3D之Character Controller(CC)与GameObject的碰撞方法
    Unity3D之资源问题处理
    Unity3D之多个fbx导入场景, 合并多个动画
    Unity3D性能优化--- 收集整理的一堆
    批量删除
    Unity3D插件之Easy Touch 3.1(1): Easy Joystick
    Unity3D与iOS消息交互方法(1)--iOS接收Unity3D发出的消息
    爬虫 requests 模块
    Flask 模型操作
    11.4 Flask session,闪现
  • 原文地址:https://www.cnblogs.com/Winston/p/1344881.html
Copyright © 2011-2022 走看看