再次深入 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 ...
随机推荐
- Chapter 21_5 替换
gsub函数的第三个参数不仅是一个字符串,还可以是一个函数或table. 如果是函数,gsub会在每次找到匹配时调用该函数.调用时的参数是捕获到的内容,该函数的返回值作为要替换的字符串. 如果是tab ...
- C# IIS7.0+ Web.Config 配置Session过期时间
1. 2. 3. <sessionState mode="InProc" timeout="120"></sessionState>
- The APR based Apache Tomcat Native library tomcat启动错误
The APR based Apache Tomcat Native library which allows optimal performance in production environmen ...
- ios下点击穿透focus获取问题
在ios下的浏览器中当点击当前页的一个按钮,用window.location.href进行跳转时,如果下一个页面里这点击按钮的位置是一个textarea或者text等那么他会触发focus事件,会出现 ...
- 用maven在eclipse用spring建javaweb工程(一)
一,步骤 只标注重点步骤: 新建工程选择Maven Project next---next---选择web工程 next---命名如下: 点击finish创建完成. 二,index.jsp和pom ...
- 洛谷-乘积最大-NOIP2000提高组复赛
题目描述 Description 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你 ...
- iOS项目导航栏返回按钮
最近iOS项目中要求导航栏的返回按钮只保留那个箭头,去掉后边的文字,在网上查了一些资料,最简单且没有副作用的方法就是 [[UIBarButtonItem appearance] setBackButt ...
- Linux GIT服务器配置
Linux下安装git插件 1. 下载git网址: https://github.com/git/git/releases 2. 放入usr/src/下 3. tar -zxvf git-** ...
- docker命令和后台参数
Docker官方为了让用户快速了解Docker,提供了一个 交互式教程 ,旨在帮助用户掌握Docker命令行的使用方法. Docker 命令行 下面对Docker的命令清单进行简单的介绍,详细内容在后 ...
- jx problem
Entity: line 37: parser error : xmlParseEntityRef: no nameecho "xxxxxx > /dev/null 2>& ...