C#特性学习笔记二
实例探讨:
自定义了一个特性类:
- [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;
- }
- }
[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;
}
}
运用特性类:
- 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;
- }
- }
- }
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;
}
}
}
处理特性类:
- 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);
- }
- }
- }
- }
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#特性学习笔记二的更多相关文章
- java8新特性学习笔记(二) 使用流(各种API)
筛选和切片 用谓词筛选,筛选出各个不相同的元素,忽略流中的头几个元素,或将流截断至指定长度 用谓词筛选 Stream接口支持filter方法,该操作接受一个谓词(返回一个boolean的函数) 作为参 ...
- java8新特性学习笔记(二) 流的相关思想
流是什么 流是Java API的新成员,他允许你以声明的方式处理数据集合,就现在来说,可以把他们看成遍历数据集合的高级迭代器.此外,流还可以透明地并行处理,你无须写任何多线程代码. 下面例子是新老AP ...
- Django学习笔记二
Django学习笔记二 模型类,字段,选项,查询,关联,聚合函数,管理器, 一 字段属性和选项 1.1 模型类属性命名限制 1)不能是python的保留关键字. 2)不允许使用连续的下划线,这是由dj ...
- ES6学习笔记<二>arrow functions 箭头函数、template string、destructuring
接着上一篇的说. arrow functions 箭头函数 => 更便捷的函数声明 document.getElementById("click_1").onclick = ...
- java8 新特性学习笔记
Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
随机推荐
- Hbase写入hdfs源码分析
版权声明:本文由熊训德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/258 来源:腾云阁 https://www.qclo ...
- Objective-C( Foundation框架 一 常见的结构体)
常见的结构体 (NSPoint,CGPoint).(NSRange,CGRange).(NSSize,CGSize) 苹果官方推荐使用CG开头的结构体 NSRange是Foundation框架中常见的 ...
- OC面向对象—封装
OC面向对象—封装 一.面向对象和封装 面向对象的三大特性:封装(成员变量).继承和多态 在OC语言中,使用@interface和@implementation来处理类. @interface就好像暴 ...
- yii2-user
https://github.com/dektrium/yii2-user 安装 : composer require "dektrium/yii2-user:0.9.*@dev" ...
- SqlServer调用CLR服务
作用 从数据库发起对外部服务的请求. 应用场景 心跳监测 定时启动站点 服务实现 代码部分(C#) using System; using System.Net; publicpartialclass ...
- 基于百度定位及天气获取的DEMO +fragment+sharedpreference
此工程较BaiduLocationXML相比:1.植入fragment,结合微信UI2.在原本主界面的button textview textview 移植到Fragment13.增加网络判断,网 ...
- 性能测试报告的指标选择、数据选择和分析的参考【以Apache AB test为例】
前几天尝试用loadrunner初试了一下性能测试,对于如何选择数据.生成数据后如何分析很是迷惑,刚刚翻看一篇网友的博客,很有条理,特此记录一下,以供参考 转自: http://liriguang.i ...
- 2-legged oauth & 3-legged oauth
3-legged oauth resource owner, client, server. resource owner 给client访问权限去访问resource owner在server上的r ...
- log4j.properties配置详解(转)
本篇文章转自http://it.oyksoft.com/log4j/ 非常感谢原创作者的辛勤编写与分享. 一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders ...
- React Native中的网络请求fetch和简单封装
React Native中的网络请求fetch使用方法最为简单,但却可以实现大多数的网络请求,需要了解更多的可以访问: https://segmentfault.com/a/1190000003810 ...