C#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作
前言:不管是自定义的一些特性,或者是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#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作的更多相关文章
- javascript 中检测数据类型的方法
typeof 检测数据类型 javascript 中检测数据类型有好几种,其中最简单的一种是 typeof 方式.typeof 方法返回的结果是一个字符串.typeof 的用法如下: typeof v ...
- 在PHP中检测一个类是否可以被foreach遍历
在PHP中,我们可以非常简单的判断一个变量是什么类型,也可以非常方便的确定一个数组的长度从而决定这个数组是否可以遍历.那么类呢?我们要如何知道这个类是否可以通过 foreach 来进行遍历呢?其实,P ...
- JS中判断数组的方法
JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...
- JS中检测数据类型的四种方法
1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...
- 【Android进阶】为什么要创建Activity基类以及Activity基类中一般有哪些方法
现在也算是刚刚基本完成了自己的第一个商业项目,在开发的过程中,参考了不少人的代码风格,然而随着工作经验的积累,终于开始慢慢的了解到抽象思想在面向对象编程中的重要性,这一篇简单的介绍一下我的一点收获. ...
- 关于spring中Assert的应用(方法入参检测工具类)
关于spring中Assert的应用(方法入参检测工具类) Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回.类似的,当我们在编写类的方法时,也常常需要对方 ...
- 百度编辑器ueditor通过ajax方式提交,不需要事先转义字符的方法(异常:从客户端(xxx)中检测到有潜在危险的 Request.Form 值)
最近项目中使用百度编辑神器ueditor,确实是很好用的一款编辑器.官网教程提供的与后端数据交互都是跟表单方式有关的,项目中使用的是ajax方式提交,因此出现了不少问题,现在记录备忘下. 环境:.ne ...
- MSIL实用指南-给字段、属性、方法、类、程序集加Attribute
C#编程中可以给字段.方法.类以及程序集加特性即继承于Attribute的类.这里讲解怎么在IL中给它们加上特性. 生成字段的对应的类是FieldBuilder,生成属性的对应的类是PropertyB ...
- JS 中检测数组的四种方法
今天和大家分享一下 JS 中检测是不是数组的四种方法,虽然篇幅不长,不过方法应该算是比较全面了. 1. instanceof 方法 instanceof 用于检测一个对象是不是某个类的实例,数组也是一 ...
随机推荐
- 用telnet和php的curl库测试http
一.telnet测试http telnet简介 Telnet协议是TCP/IP协议族的其中之一,是Internet远端登录服务的标准协议和主要方式,常用于网页服务器的远端控制,可供使用者在本地 ...
- BF算法
BF算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符:若不相等,则比较S的第二个字符和P的第一个字符, ...
- css3 forwards、backwards、both
animation-fill-mode 属性规定动画在播放之前或之后,其动画效果是否可见. none 不改变默认行为. forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义). ...
- camera render texture 游戏里的监控视角
Camera里: 新建render texture并拖入到target texture里 新建材质球 拖入render texture camera里的视角会在材质球上出现 新建一个pla ...
- How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system
type command - env x='() { :;}; echo vulnerable' bash -c 'echo hello' in your terminal. if your sy ...
- 802.11 wireless 1(主要还是学习ccna wireless的体系)
802.11 wireless 1(主要还是学习ccna wireless的体系)ISM频带(ISM band starts early 1990s)900MHZ 2.4GHZ 5GHZ 四种 ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- hdu 4240 Route Redundancy 最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 A city is made up exclusively of one-way steets. ...
- C# Winform常见的Editor及其它经验
1.新建一个自定义Editor,继承自.NET自带的Editor,override某些方法,再附加到属性中: public class MyCollectionEditor : CollectionE ...
- swipejs的使用
<div id='slider' class='swipe'> <div class="swipe-wrap"> <div><img sr ...