博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CLR Via CSharp读书笔记(18):定制Attributes
阅读量:6907 次
发布时间:2019-06-27

本文共 954 字,大约阅读时间需要 3 分钟。

检测定制的attribute:

定制attribute之后,主要在于检测。主要的方法有System.TypeIsDefined方法,System.Attribute类定义的三个静态方法用以获取与一个目标关联的attribute: IsDefined, GetCustomAttributes(用于将AllowMultiple设为true的attribute,或者列出所有已应用的attribute)和GetCustomAttribute(通常用于将AlloMultiple设为false的attribute).

检测时不创建从Attribute派生的对象

使用System.Reflection.CustomAttributeData类的GetCustomAttributes方法,有四个重载版本,分别接受Assembly, Module, ParameterInfoMemberInfo. 通常先用Assembly的静态方法ReflectionOnlyLoad加载一个程序集,再用CustomAttributeData类分析这个程序集的元素据中的attribute。

条件attribute类:

// #define TEST#define VERIFYusing System;using System.Diagnostics;[Conditional("TEST")][Conditional("VERIFY")]public sealed class CondAttribute : Attribute {}[Cond]public static class Program{     public static void Main() {      Console.WriteLine("CondAttribute is {0}applied to Program type.",         Attribute.IsDefined(typeof(Program), typeof(CondAttribute)) ? "" : "not ");   }}

 

 

 

转载于:https://www.cnblogs.com/thlzhf/archive/2012/12/06/2805480.html

你可能感兴趣的文章
转:【高并发简单解决方案】redis缓存队列+mysql 批量入库+php离线整合
查看>>
列表和屏幕的相互调用之从列表调用屏幕
查看>>
loadrunner-2-13错误处理函数
查看>>
C# 中的 App.config 文件配置
查看>>
LeetCode 队列与BFS--岛屿的数量
查看>>
jmeter-noguimodel
查看>>
Git使用++
查看>>
实现DataGridView和DevExpress.GridControl表头全选功能
查看>>
读《构建之法》十一,十二章有感
查看>>
Storyboard全解析(1)
查看>>
[C++]基本类型转换
查看>>
清除windows系统垃圾文件简易脚本(bat)
查看>>
TCP之close_wait
查看>>
spring boot. pom
查看>>
Android WindowManager实现悬浮窗效果 (一)——与当前Activity绑定
查看>>
陶哲轩实分析 定理7.5.1 (方根判别法) 证明
查看>>
The direct sum of functions
查看>>
微软职位内部推荐-Senior Software Engineer
查看>>
《Linux内核设计与实现》读书笔记(十五)- 进程地址空间(kernel 2.6.32.60)
查看>>
codevs1026
查看>>