ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.NET中的Attribute(二)----理解

2019-08-02 22:39:24  阅读:262  来源: 互联网

标签:自定义 pet Attribute 理解 Animal NET AnimalTypeAttribute public


原文链接:http://www.cnblogs.com/BaiYong/archive/2008/03/18/1110712.html

在前一篇文章我介绍了Attribute类的认识,现在我们更深入理解它,看看如何自定义Attribute类。
首先看看下面的例子from msdn:
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.
 */

分析:
1、首先定义了一个枚举,它可以看成是可以关联到目标元素的属性(Attribute)信息,从而将自定义信息绑定到目标元素上。
2、定义自己的Attribute,这里所设定的属性Attribute要求目标元素要有一个pet(相当于是要有一个参数)以便进行正需的操作,这样就达到了对程序运行是的控制。这个类主要是一个构造函数和一个Property。(这种结构类似前面自定义配置节处理程序的结构)
   这里又要注意了,
(1)定义类的时候名称为AnimalTypeAttribute,而使用的时候变成了AnimalType这是允许的,因为Attribute的命名规范,也就是你的Attribute的类名+"Attribute",当你的Attribute施加到一个程序的元素上的时候,编译器先查找你的Attribute的定义,如果没有找到,那么它就会查找“Attribute名称"+Attribute的定义。如果都没有找到,编译器才就报错。
(2)由于是根据pet进行程序控制,换句话说程序是根据Attribute值pet来控制目标的执行的,那么在自定义的Attribute类中就要定义一个Property 类型为Animal,同时成员变量定义为内部访问,即protected Animal thePet;(注意protected关键字);
(3)自定义AnimalTypeAttribute类的构造函数是在该Attribute被设置后调用的。
3、定义一个需要由Attribute来控制执行各种方法的类AnimalTypeTestClass,比如在现实生活中我们根据红绿灯判断是前进还是等待等,方法上面有了自定义的Attribute的设置,这样在编译的时候自定义的AnimalTypeAttribute类被实例化了,也就是AnimalTypeAttribute中的构造函数被调用,从而知道pet到底是什么。
4、编写测试类,在这个类中我们首先实例化一个AnimalTypeTestClass类,然后得到这个类的类型信息(Type),再列出这个类中的所有方法,在根据些方法得到方法上的所有Attribute类(这里的Attribute还不一定全部是AnimalTypeAttribute,因为前面已经说了,在目标元素上可能不只一个Attribute),如果
Attibute类就是自己定义的AnimalTypeAttribute类,就直接输出方法的名字和上面施加的对应自定义Attribute的具体值(也就是属性值)。

这里这是谈了自定义Attribute类的简单情况,后面我还会继续说明一些比较复杂的情况。我力求根据我的理解将每一个细节都能讲到,希望你看了不觉得我啰嗦,因为我是初学者,所以还是老老实实记录下来,以便以后参考^_^

转载于:https://www.cnblogs.com/BaiYong/archive/2008/03/18/1110712.html

标签:自定义,pet,Attribute,理解,Animal,NET,AnimalTypeAttribute,public
来源: https://blog.csdn.net/weixin_30247781/article/details/98248059

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有