前言:不管是自定义的一些特性,或者是C#中内置的特性,均继承自Attribute这个类,这个类也提供了一些方法,方便我们使用。

Attribute类有三个静态方法:
1.IsDefined,如果有指定的Attribute的实例与目标关联,则返回true。
2.GetCustomAttributes,返回一个数组,其中每个元素都是应用于目标的指定attribute类的一个实例。该方法通常用于已将AllowMultiple设为true的attribute,或者用于列出已应用的所有attribute。
3.GetCustomAttribute返回应用于目标的指定attribute类的一个实例。该方法通常用于已将AllowMultiple设置为false的attribute。

P.S.如果仅仅定义一个特性或者说给某个部分应用了特性是没有意义的,我们通过判断操作来对逻辑进行控制。

一、检测元素是否包含指定的特性

1.使用最简单的方法,就是使用上述中的IsDefined方法,最简单,使用如下(MSDN例子):

 public class TestClass
{
//表明此方法过时
[Obsolete("This method is obsolete. Use Method2 instead.")]
public void Method1()
{}
public void Method2()
{}
}
 static void Main(string[] args)
{
// 得到指定类型的Type对象
Type clsType = typeof(TestClass);
// 得到当前Method1方法的方法对象MethodInfo
MethodInfo mInfo = clsType.GetMethod("Method1");
// 判断当前的方法是否应用了ObsoleteAttribute特性
bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
// 输出结果
Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
isDef ? "is" : "is not", mInfo.Name, clsType.Name);
// 如果存在此特性
if (isDef)
{
      //得到当前MethodInfo中的ObsoleteAttribute特性对象
ObsoleteAttribute obsAttr =
(ObsoleteAttribute)Attribute.GetCustomAttribute(
mInfo, typeof(ObsoleteAttribute));
if (obsAttr != null)
Console.WriteLine("The message is: \"{0}\".",
obsAttr.Message);
else
Console.WriteLine("The message could not be retrieved.");
}
}
}

2.输出某个类的所有特性,例子如下:

   [assembly:CLSCompliant(true)]
[Serializable]
[DefaultMemberAttribute("Main")]
[DebuggerDisplayAttribute("Richer", Name = "Super.Mario", Target = typeof(Program))]
public sealed class Program
{
public Program() { }
[CLSCompliant(true)]
[STAThread]
public static void Main(string[] args)
{
ShowAttributes1(typeof(Program)); MemberInfo[] members = typeof(Program).FindMembers(MemberTypes.Constructor | MemberTypes.Method,
BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static,
Type.FilterName, "*");
foreach (var member in members)
{
ShowAttributes1(member);
}
Console.Read();
} static void ShowAttributes1(MemberInfo attributesTarget)
{
Attribute[] attributes = Attribute.GetCustomAttributes(attributesTarget);
Console.WriteLine("Attributes applied to {0} : {1}", attributesTarget.Name, (attributes.Length == ? "None" : string.Empty)); foreach (var attribute in attributes)
{
Console.WriteLine("{0}", attribute.GetType().ToString());
if (attribute is DefaultMemberAttribute)
{
Console.WriteLine("MemberName={0}", ((DefaultMemberAttribute)attribute).MemberName);
}
if (attribute is ConditionalAttribute)
{
Console.WriteLine("ConditionString={0}", ((ConditionalAttribute)attribute).ConditionString);
}
if (attribute is CLSCompliantAttribute)
{
Console.WriteLine("IsCompliant={0}", ((CLSCompliantAttribute)attribute).IsCompliant);
}
var dda = attribute as DebuggerDisplayAttribute;
if (dda != null)
{
Console.WriteLine("Value={0},Name={1},Target={2}", dda.Value, dda.Name, dda.Target);
}
}
}
  }
}

不用太在意某一个特性是什么意思,旨在通过Type.FindMembers方法得到所有的成员,然后输出特性的信息。当然这里最主要的方法是GetCustomAttributes,上文已经解释过,用于获取指定对象的所有应用的特性,具体解释可以参看MSDN,最后将一个个特性的信息输出。

二、通过更为安全的方式进行特性的操作

之所以说是安全的方式操作,那是因为在调用Attribute的GetCustomAttribute或者GetCustomeAttributes方法时,这些方法会在内部调用attribute类的构造器,而且可能调用属性的set访问器方法。除此之外,首次访问一个类型会造成CLR调用类型的类型构造器(如果有)。在构造器、set访问器方法以及类型构造器中,可能包含每次查找一个attribute时都要执行的代码。这样的话,就相当于允许未知的代码在AppDomain中运行,所以是一个潜在的安全隐患。
使用System.Reflection.CustomAttributeData类,可以在查找attribute的同时禁止执行attribute中的代码。这个类定义了一个静态方法GetCustomeAttribute来获取与一个目标关联的attribute。该方法有四个重载版本:一个接受一个Assembly,一个接受一个Module,一个接受一个ParameterInfo,另一个接受MemberInfo。GetCustomAttributes方法相当于一个工厂方法,返回一个CustomAttributeData类型的集合。在集合中应用于指定目标的每个定制Attribute都有一个对应的元素。针对每个CustomAttributeData对象,都可以查询一些只读属性,判断attribute对象时如何构造和初始化的。具体地说,Constructor属性指出构造器方法要如何调用。ConstructorArguments属性以一个IList<CustomAttributeTypedArgument>实例的形式返回要传给这个构造器的实参。NamedArguments属性以一个IList<CustomAttributeNamedArgument>实例的形式,返回要设置的字段或者属性。注意,之所以说要,是因为不会实际地调用构造器和set访问器方法。

 [assembly:CLSCompliant(true)]
[Serializable]
[DefaultMemberAttribute("Main")]
[DebuggerDisplayAttribute("Richer", Name = "Super.Mario", Target = typeof(Program))]
public sealed class Program
{
public Program() { }
[CLSCompliant(true)]
[STAThread]
public static void Main(string[] args)
{
ShowAttributes1(typeof(Program)); MemberInfo[] members = typeof(Program).FindMembers(MemberTypes.Constructor | MemberTypes.Method,
BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static,
Type.FilterName, "*");
foreach (var member in members)
{
ShowAttributes2(member);
}
Console.Read();
}
static void ShowAttributes2(MemberInfo attributeTarget)
{//通过禁止执行attribu类的任何方法,我们获得了增强的安全性。
IList<CustomAttributeData> attributes = CustomAttributeData.GetCustomAttributes(attributeTarget);
Console.WriteLine("Attributes applied to {0} : {1}", attributeTarget.Name, (attributes.Count == ? "None" : string.Empty));
foreach (var attribute in attributes)
{
//显示所应用的每个Attribute的类型
Type t = attribute.Constructor.DeclaringType;
Console.WriteLine("{0}",t.ToString());
Console.WriteLine("Constructor called={0}", attribute.Constructor); IList<CustomAttributeTypedArgument> posArgs = attribute.ConstructorArguments;
Console.WriteLine("Positional arguments passed to constructor:"+
((posArgs.Count==)? " None" : string.Empty));
foreach (var arg in posArgs)
{
Console.WriteLine(" Type={0},Value={1}", arg.ArgumentType, arg.Value);
} IList<CustomAttributeNamedArgument> namedArgs = attribute.NamedArguments;
Console.WriteLine(" Named arguments set after construction:" + ((namedArgs.Count == ) ? "None" : string.Empty));
foreach (var namedArg in namedArgs)
{
Console.WriteLine(" Name={0},Type={1},Value={2}", namedArg.MemberInfo.Name, namedArg.TypedValue.ArgumentType,
namedArg.TypedValue.Value);
} }
}
}

此种方式前半部分和上一种方法一样,得到类型的成员,然后通过方法进行输出显示。区别就在于获取特性的过程使用了CustomAttributeData对象得到指定成员的特性集合,然后通过ConstructorArguments得到参数列表(获取为由 CustomAttributeData 对象表示的特性实例指定的位置参数列表,也就是构造函数的参数。)。同时也通过NamedArguments属性得到特性的位置参数(获取为由 CustomAttributeData 对象表示的特性实例指定的命名参数列表。),也可以理解为就是一般的属性(即不是通过构造函数传递的)。

好了,本文主要简单介绍了如何去检测C#中各种成员是否包含指定的特性以及得到成员的所有特性,然后做出相应的操作。如果大家有好的方法,可以及时讨论哦。

C#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作的更多相关文章

  1. javascript 中检测数据类型的方法

    typeof 检测数据类型 javascript 中检测数据类型有好几种,其中最简单的一种是 typeof 方式.typeof 方法返回的结果是一个字符串.typeof 的用法如下: typeof v ...

  2. 在PHP中检测一个类是否可以被foreach遍历

    在PHP中,我们可以非常简单的判断一个变量是什么类型,也可以非常方便的确定一个数组的长度从而决定这个数组是否可以遍历.那么类呢?我们要如何知道这个类是否可以通过 foreach 来进行遍历呢?其实,P ...

  3. JS中判断数组的方法

    JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...

  4. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  5. 【Android进阶】为什么要创建Activity基类以及Activity基类中一般有哪些方法

    现在也算是刚刚基本完成了自己的第一个商业项目,在开发的过程中,参考了不少人的代码风格,然而随着工作经验的积累,终于开始慢慢的了解到抽象思想在面向对象编程中的重要性,这一篇简单的介绍一下我的一点收获. ...

  6. 关于spring中Assert的应用(方法入参检测工具类)

    关于spring中Assert的应用(方法入参检测工具类) Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回.类似的,当我们在编写类的方法时,也常常需要对方 ...

  7. 百度编辑器ueditor通过ajax方式提交,不需要事先转义字符的方法(异常:从客户端(xxx)中检测到有潜在危险的 Request.Form 值)

    最近项目中使用百度编辑神器ueditor,确实是很好用的一款编辑器.官网教程提供的与后端数据交互都是跟表单方式有关的,项目中使用的是ajax方式提交,因此出现了不少问题,现在记录备忘下. 环境:.ne ...

  8. MSIL实用指南-给字段、属性、方法、类、程序集加Attribute

    C#编程中可以给字段.方法.类以及程序集加特性即继承于Attribute的类.这里讲解怎么在IL中给它们加上特性. 生成字段的对应的类是FieldBuilder,生成属性的对应的类是PropertyB ...

  9. JS 中检测数组的四种方法

    今天和大家分享一下 JS 中检测是不是数组的四种方法,虽然篇幅不长,不过方法应该算是比较全面了. 1. instanceof 方法 instanceof 用于检测一个对象是不是某个类的实例,数组也是一 ...

随机推荐

  1. Java Day 16

    基本数据类型包装类 Integer.MAX_VALUE  Integer.parseInt(); intValue(); valueOf(); 自动装拆箱 如果是一个字节范围,数据共享 字符串中 数值 ...

  2. SQL Server 2008 的安装

    SQL Server 2008简体中文企业版下载(SQL2008) SQL Server 2008分为SQL Server 2008企业版.标准版.工作组版.Web版.开发者版.Express版.Co ...

  3. 小组开发项目--NABC分析

    我们小组--女神经们,开发项目是重力解锁,我认为我们的项目的最大特点就是不使用开锁键唤醒屏幕.下面我将针对这一特点进行NABC分析: N:经调查一部分人群的手机不能使用就是开机键坏了,我们就是针对这一 ...

  4. C++编程显示四则运算题目

    题目:C++编程显示四则运算题目 设计思路:(1)让用户自己确定出题的数量,同时显示加减乘除四则运算. (2)考虑到用户可能只会一种运算,因此可以选择运算.

  5. 我教女朋友学编程html系列(5) html中table的用法和例子

    女朋友不是学计算机的,但是现在从事计算机行业,做技术支持,她想学习编程,因此我打算每天教她一点点,日积月累,带她学习编程,如果其他初学者感兴趣,可以跟着学. 为了将table介绍的简单.生动,具有实战 ...

  6. ACK

    ACK (Acknowledgement),即确认字符,在数据通信中,接收站发给发送站的一种传输类控制字符.表示发来的数据已确认接收无误. 目录 1基本介绍 2详细释义     1基本介绍编辑 英文缩 ...

  7. HDU 5592 ZYB's Premutation

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  8. 设计模式之职责链模式(Chain of Responsibility)

    职责链模式原理: 职责链模式和装饰模式以及组合模式类似的地方是都维持着指向父类的指针, 不同点是职责链模式每个子类都继承父类的指针及每个之类都维持着指向父类的指针,而组合模式与装饰模式是组合类鱼装饰类 ...

  9. BZOJ 2301 Problem b

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 冬令营听了莫比乌斯,这就是宋老师上课讲的例题咯[今天来实现一下] #include& ...

  10. 虚拟目录里面的webconfig不继承网站的设置

    必須在上一层虚拟目录(如根目录,上级网站)所在的Web.config加上 如:<location path="." allowOverride="false&quo ...