再次深入 C# Attribute
- 了解attribute
Attribute 只是将一些附加信息与某个目标元素关联起来的方式。
Attribute 是一个类,这个类可以提供一些字段和属性,不应提供公共方法,事件等。在定义attribute类的构造方法,字段和属性时,对数据类型有严格的要求,一般要求为: Boolean, Char, Byte, Sbyte, Int16, UInt16, Int32, Int64,Single, Double, String, Type, Object, Enum, 可以使用数组,但是并不提倡使用。
使用attribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false)]
[Conditional("Specify")]
public class DisplayNameAttribute : Attribute
{
public string PropertyName { get; set; } public DisplayNameAttribute()
{ } public DisplayNameAttribute(string propertyName)
{
this.PropertyName = propertyName;
}
}
- 检测attribute
- attribute定义和应用之后还没有任何意义,只有在运行的时候去检测,然后根据对应的attribute做一些不同事情才让attribute变得有意义,那么如何检测attribute呢?
|
方法名称 |
说明 |
|
IsDefined |
如果至少有一个指定的Attribute派生类的实例与目标关联,就返回true。这个方法效率很高,因为他不构造(反序列化)attribute类的任何实例 |
|
GetCustomAttributes |
返回一个数组,其中每个元素都是应用于目标的指定attribute类的一个实例。 如果不为这个方法指定具体的attribute类,数组中包含的就是已应用的所有attribute的实例,不管他们是什么类。每个实例都使用编译时指定的参数、字段和属性来构造(反序列化)。如果目标没有应用任何attribute类的实例,就返回一个空数组。该方法通常用于已将AllowMultiple 设为true的attribute |
|
GetCustomAttribute |
返回应用于目标的指定attribute类的一个实例。实例使用编译是指定的参数、字段和属性来构造(反序列化)。如果目标没有应用任何attribute类的实例,就返回null。如果目标应用了制定attribute的多个实例,抛出异常AmbiguousMatchException. 因此该方法通常用于AllowMultiple=false的attribute |
最后的例子中展示了如何使用这些方法。
2. 上面的方法在检测时进行了attribute对象的构造(需要反序列化),这样有可能调用set访问器和构造方法,可能出现安全风险(未知代码在AppDomain中运行)。因此可以使用CustomAttributeData类中定的静态方法进行检测
public override string ToString()
{
//在子类中对父类中的方法进行重写,使用不创建attribute对象不执行代码(反序列化)的方法
StringBuilder value = new StringBuilder();
if (!this.GetType().IsClass)
return value.ToString();
PropertyInfo[] properties = this.GetType().GetProperties(); foreach (var property in properties)
{
string propertyName = property.Name;
IList<CustomAttributeData> list = CustomAttributeData.GetCustomAttributes(property);
foreach (var item in list)
{
if (item.Constructor.DeclaringType == typeof(DisplayNameAttribute))
{
Console.Write("This property applies DisplayNameAttribute.");
}
}
}
return base.ToString();
}
- 条件attribute
应用attribute时生成元数据,如果想让attribute在编译是不生成元数据可以使用条件编译,下面是例子
#define Specify
//#define UnSpecify namespace Attribute
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics; public class Pepople
{
[DisplayName("姓名")]
[TestDemoAttribute]
public string Name { get; set; }
} public class Man : Pepople
{
[DisplayName("年龄")]
public int Age { get; set; } public float Height { get; set; }
} [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false)]
[Conditional("Specify")]
public class DisplayNameAttribute : Attribute
{
public string PropertyName { get; set; } public DisplayNameAttribute()
{ } public DisplayNameAttribute(string propertyName)
{
this.PropertyName = propertyName;
}
} [Conditional("UnSpecify")]
public class TestDemoAttribute : Attribute
{ }
}
条件attribute
注意:赘述一句,在C#中进行预定义时,必须在文件在最前面
我们用ildasm查看元数据得知,TestDemoAttribute 没有被编译成员数据,而DisplayNameAttribute被编译成了元数据
- 实例
对上面提到的知识写了个例子,以便加强知识的理解运用。
#define Specify
//#define UnSpecify namespace Attribute
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics; public class Pepople
{
[DisplayName("姓名")]
[TestDemoAttribute]
public string Name { get; set; } public override string ToString()
{
StringBuilder value = new StringBuilder();
if (!this.GetType().IsClass)
return value.ToString();
PropertyInfo[] properties = this.GetType().GetProperties(); foreach (var property in properties)
{
string propertyName = property.Name;
if (property.IsDefined(typeof(DisplayNameAttribute), false))
{
DisplayNameAttribute attr = (DisplayNameAttribute)Attribute.GetCustomAttribute(property, typeof(DisplayNameAttribute));
if (attr != null)
{
propertyName = attr.PropertyName;
}
}
value.Append(propertyName);
value.Append("\t");
value.Append(property.GetValue(this, null));
value.Append("\n\r");
} return value.ToString();
}
} public class Man : Pepople
{
[DisplayName("年龄")]
public int Age { get; set; } public float Height { get; set; }
} public static class OutputerClass
{
public static string StringClass(object obj)
{
StringBuilder value = new StringBuilder();
if (!obj.GetType().IsClass)
return value.ToString();
PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (var property in properties)
{
DisplayNameAttribute attr = (DisplayNameAttribute)Attribute.GetCustomAttribute(property, typeof(DisplayNameAttribute));
if (attr != null)
{
value.Append(attr.PropertyName);
value.Append("\t");
value.Append(property.GetValue(obj, null));
value.Append("\n\r");
}
} return value.ToString();
}
} //可以用attributeUsage声明属性应用的范围,可以多选,多选的时候|运算即可
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false)]
[Conditional("Specify")]
public class DisplayNameAttribute : Attribute
{
public string PropertyName { get; set; } public DisplayNameAttribute()
{ } public DisplayNameAttribute(string propertyName)
{
this.PropertyName = propertyName;
}
} [Conditional("UnSpecify")]
public class TestDemoAttribute : Attribute
{ }
}
实例
再次深入 C# Attribute的更多相关文章
- C#基础---Attribute(标签) 和 reflect(反射) 应用二
以前我有写过一篇有关,打标签和反射的应用,主要用于类中字段的验证.下面是连接 C#基础---Attribute(标签) 和 reflect(反射) 应用. 这个项目迭代发现公司项目里面发现老代码对业务 ...
- 有关attribute和property,以及各自对select中option的影响
这个问题老生常谈,但是直到现在我依旧时常会把它搞混.下面列一些各自的特性. attribute property 设置方法 option.setAttribute('selected', true ...
- OAF_开发系列06_实现OAF属性集的介绍和开发Attribute Set(案例)
20150705 Created By BaoXinjian
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【外传】——Attribute Routing
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 题外话:由于这个技术点是新学的,并不属于原系列,但借助了原系列的项目背景,故命名外传系列,以后也可 ...
- .net学习之Attribute特性和EF关键知识点
一.Attribute特性/标签1.Attribute用来对类.属性.方法等标注额外的信息,贴一个标签简单的说,定制特性Attribute,本质上就是一个类,它为目标元素提供关联附加信息,并在运行时以 ...
- Attribute "lazy" with value "true" must have a value from the list "false proxy no-proxy "
Hibernate 3.2 版本 当设置lazy="true"属性时,会产生该个异常: Attribute "lazy" with value "tr ...
- 深入浅出Attribute (转载)
原文地址:http://blog.csdn.net/FantasiaX/article/details/1627694 正文: 什么是Attribute?Attribute是干什么使的?Attribu ...
- Flash Attribute
参考:http://www.open-open.com/lib/view/open1397266120028.html 为解决POST/Forward/GET出现的重复提交数据问题,改用POST/Re ...
- C#中的Attribute和Java中的Annotation
在之前的博客中介绍过C#的Attribute(特性),简单的说,特性主要就是利用反射技术,在运行期获取关注类的相关标注信息,然后利用这些标注信息对关注的类进行处理,最近因为工作的原因,需要看一下Jav ...
随机推荐
- bitcode 关于讯飞
在真机调试的时候一直报 ld: '/Users/Chenglijuan/Documents/语音识别/lib/iflyMSC.framework/iflyMSC(IFlyRecognizerView. ...
- python3数据结构
列表 list.append(x) 把一个元素添加到列表的结尾,相当于a[len(a):]=[x] list.extend(L) 将一个给定列表中的所有元素都添加到另一个列表中,相当于a[(len): ...
- 用Redis作为Mysql数据库的缓存【转】
用Redis作Mysql数据库缓存,必须解决2个问题.首先,应该确定用何种数据结构存储来自Mysql的数据:在确定数据结构之后,还要考虑用什么标识作为该数据结构的键. 直观上看,Mysql中的数据都是 ...
- eclipse安装插件的4种方式
Eclipse插件的安装方法大体有以下三种:[9] 第一种:直接复制法 假设Eclipse的安装目录在C:\eclipse,解压下载的eclipse 插件或者安装eclipse 插件到指定目录AA(如 ...
- flex属性值----弹性盒子布局
里面的属性都 是在有display: flex的情况 下才生效. 兼容性写法: display: -webkit-box; /*老版本语法: Safari, iOS, Android browse ...
- Elasticsearch相关配置(二)
一.关于elasticsearch的基本概念 term 索引词,在elasticsearch中索引词(term)是一个能够被索引的精确值.foo,Foo Foo几个单词是不相同的索引词.索引词(ter ...
- Java资源大全中文版
awesome-java-cn 是 Java 资源大全的中文版,包括开发库.开发工具.网站.博客等,将由伯乐在线持续更新. https://github.com/jobbole/awesome-jav ...
- php笔记(三)PHP类和对象之访问控制
访问控制通过关键字public,protected和private来实现.被定义为公有的类成员可以在任何地方被访问.被定义为受保护的类成员则可以被其自身以及其子类和父类访问.被定义为私有的类成员则只能 ...
- 可用fidder测试的一些安全测试点
以下是整理的一些常见的安全渗透测试点 1.用工具fidder抓包拦截篡改服务器端返回的代码,导致下级拥有对上级的访问操作权限 以下是公司开发写的用户角色权限页面跳转 修改普通角色跳转的页面为管理员跳转 ...
- JavaScript Date对象更进一步
总结分享这个近期开发解决的一个Bug. Javascript的Date对象具有容错性,会自动根据当年的日期根据设置的属性值转换,也就是说Date对象的setDate会影响setMonth,month会 ...