实例探讨:

自定义了一个特性类:

  1. [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
  2. class WahAttribute:System.Attribute
  3. {
  4. private string description;
  5. public string Description
  6. {
  7. get { return description; }
  8. set { description = value; }
  9. }
  10. private string author;
  11. public string Author
  12. {
  13. get { return author; }
  14. set { author = value; }
  15. }
  16. public WahAttribute(string desc)
  17. {
  18. this.description = desc;
  19. }
  20. }

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
class WahAttribute:System.Attribute
{
private string description;

public string Description
{
get { return description; }
set { description = value; }
}
private string author;

public string Author
{
get { return author; }
set { author = value; }
}
public WahAttribute(string desc)
{
this.description = desc;
}
}

运用特性类:

  1. namespace attributeDemo
  2. {
  3. public class Teacher
  4. {
  5. private string name;
  6. public string Name
  7. {
  8. get { return name; }
  9. set { name = value; }
  10. }
  11. private int age;
  12. public int Age
  13. {
  14. get { return age; }
  15. set { age = value; }
  16. }
  17. private string sex;
  18. public string Sex
  19. {
  20. get { return sex; }
  21. set { sex = value; }
  22. }
  23. //只有用户名为wah的才可以调用此方法
  24. [Wah("this is my attribute test", Author = "wah", Description = "test")]
  25. public string getMessage()
  26. {
  27. return "好好学习,天天向上";
  28. }
  29. [Wah("this is with parameters test",Author="wanggaihui",Description="test with parameters")]
  30. public int Test(int a,int b)
  31. {
  32. return a+b;
  33. }
  34. }
  35. }

namespace attributeDemo
{
public class Teacher
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private int age;

public int Age
{
get { return age; }
set { age = value; }
}
private string sex;

public string Sex
{
get { return sex; }
set { sex = value; }
}
//只有用户名为wah的才可以调用此方法
[Wah("this is my attribute test", Author = "wah", Description = "test")]
public string getMessage()
{
return "好好学习,天天向上";
}
[Wah("this is with parameters test",Author="wanggaihui",Description="test with parameters")]
public int Test(int a,int b)
{
return a+b;
}
}
}

处理特性类:

  1. private void button_Click(object sender, EventArgs e)
  2. {
  3. //得到类型
  4. Type type = typeof(Teacher);
  5. //得到此类型所有方法
  6. MethodInfo[] methods = type.GetMethods();
  7. foreach (MethodInfo method in methods)
  8. {
  9. //得到此方法的所有特性
  10. object[] attributes = method.GetCustomAttributes(false);
  11. foreach (object o in attributes)
  12. {
  13. //判断是否是自己定义的特性
  14. if (o.GetType() == typeof(WahAttribute))
  15. {
  16. //强转取得值
  17. WahAttribute waha = (WahAttribute)o;
  18. this.listBox1.Items.Add("author=" + waha.Author);
  19. this.listBox1.Items.Add("description=" + waha.Description);
  20. }
  21. }
  22. }
  23. }

private void button_Click(object sender, EventArgs e)
{
//得到类型
Type type = typeof(Teacher);
//得到此类型所有方法
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
//得到此方法的所有特性
object[] attributes = method.GetCustomAttributes(false);
foreach (object o in attributes)
{
//判断是否是自己定义的特性
if (o.GetType() == typeof(WahAttribute))
{
//强转取得值
WahAttribute waha = (WahAttribute)o;
this.listBox1.Items.Add("author=" + waha.Author);
this.listBox1.Items.Add("description=" + waha.Description);
}
}
}
}

C#特性可以应用于各种类型和成员。加在类前面的是类特性,加在方法前面的是方法特性。无论他们被用在哪里,无论他们之间有什么区别,特性的最主要的目的就是自描述。并且因为特性是可以由自己定制的,而不仅仅局限于.net提供的那几个现成的,因此给C#程序开发带来了很大的灵活性。

C#特性学习笔记二的更多相关文章

  1. java8新特性学习笔记(二) 使用流(各种API)

    筛选和切片 用谓词筛选,筛选出各个不相同的元素,忽略流中的头几个元素,或将流截断至指定长度 用谓词筛选 Stream接口支持filter方法,该操作接受一个谓词(返回一个boolean的函数) 作为参 ...

  2. java8新特性学习笔记(二) 流的相关思想

    流是什么 流是Java API的新成员,他允许你以声明的方式处理数据集合,就现在来说,可以把他们看成遍历数据集合的高级迭代器.此外,流还可以透明地并行处理,你无须写任何多线程代码. 下面例子是新老AP ...

  3. Django学习笔记二

    Django学习笔记二 模型类,字段,选项,查询,关联,聚合函数,管理器, 一 字段属性和选项 1.1 模型类属性命名限制 1)不能是python的保留关键字. 2)不允许使用连续的下划线,这是由dj ...

  4. ES6学习笔记<二>arrow functions 箭头函数、template string、destructuring

    接着上一篇的说. arrow functions 箭头函数 => 更便捷的函数声明 document.getElementById("click_1").onclick = ...

  5. java8 新特性学习笔记

    Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...

  6. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  7. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  8. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

  9. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

随机推荐

  1. 超级迷宫 nabc

    特点之一:益智模式 N  我们的游戏需要一点点益智答题使其精彩 A 在走迷宫的过程中,会遇到某一个点,出现一个益智小问题,答对即可通过 B 增加游戏的趣味性,吸引用户 C 答题游戏不少,前不久腾讯的手 ...

  2. 7 -- Spring的基本用法 -- 7...

    7.7 创建Bean的3种方式 ① 调用构造器创建Bean. ② 调用静态工厂方法创建Bean. ③ 调用实例工厂方法创建Bean. 7.7.1 使用构造器创建Bean实例. 使用构造器来创建Bean ...

  3. CSS Hack相关知识

    CSS Hack 1.由于不同厂商的浏览器,比如Internet Explorer,Safari,Chrome,Mozila Firefox等,或者是同一厂商的浏览器的不同版本,如IE6和IE7,对C ...

  4. [笔记]使用clearfix清除浮动

    转载自奶牛博客 .clearfix { *zoom: 1; } .clearfix:before, .clearfix:after { display: table; line-height: 0; ...

  5. 使用Python创建简单的HTTP和FTP服务

    不管工作中还是其他场合,经常会有文件分享的需求,比如自己下了一个4GB的游戏,同事下了一个800MB的软件,其他人如果也需要这些文件,显然直接分享是最快捷.最方便.最环保的方式了,如果再重新下,既浪费 ...

  6. oracle sql

    show user desc 'table' SELECT DISTINCT SELECT * FROM emp WHERE comm is NOT NULL; SELECT * FROM emp W ...

  7. Windows Store App 主题动画

    Windows 8系统的动画库中包含了丰富的主题动画,在开发Windows应用商店应用时,使用主题动画编写较少的代码即可实现所期望的动画效果.下面介绍一些常用的主题动画,读者可以根据每种主题动画提供的 ...

  8. tslib1.4与Qt4.8.6的交叉编译与移植

    最近开始正式接触QT开发,网上看了些移植教程都写的有点乱,博客园的emouse思·睿有一篇写的很好的文章,下面是文章的链接 http://www.cnblogs.com/emouse/archive/ ...

  9. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  10. java中Commons-fileupload实现上传

    java中Commons-fileupload组件实现上传 在实现功能之前需要导入两个jar文件,分别是 commons-fileupload-1.3.1.jar 和 commons-io.jar 文 ...