再次深入 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 ...
随机推荐
- 关于java集合排序
对于排序,java开发者并不陌生. 为避免以后遗忘,现在再次总结一下! 常见8大排序算法, 平时自己熟悉的只有几种种!冒泡,二分/折半.插入.快排等!现在一一讲解一下,这里只讲思想,暂时不做实现! 一 ...
- 关于数据汇总方面返回Json数据的小小心得
在一开始的开发中,计算好相关数据,然后通过 1.拼串 2.实例化Dictory对象再通过JavaScriptSerializer转换为json. 其中,2只适合于二维数据.1适合多维数据,但拼串比较费 ...
- trove,测试,db小解析
# Copyright 2014 Tesora Inc.# All Rights Reserved.## Licensed under the Apache License, Version 2.0 ...
- mmmmmmmm
// // AView.m // AutoLayout // // Created by ZhuYi on 16/5/24. // Copyright © 2016年 ZY. All rights r ...
- hdu1536Nim
sg函数打表的基础应用,第一道ac的sg函数打表题纪念下,直接上代码: hdu1536题目连接 #include<iostream> #include<cstdio> #inc ...
- myeclipse连接数据库oracle(添加jdbc.properties)
第一步:在src下面建一个包com.xsl.conf 第二步:在建好的包下面新建一个jdbc.properties 第三步:在jdbc.properties里写入内容如下: driver = orac ...
- UIBezierPath与CAShapeLayer结合画扇形
/*让半径等于期望半径的一半 lineWidth等于期望半径 就可以画圆*/ 可以看出layer的走势是从圆边的中间一半在圆外 一半在圆内 因此让半径等于期望半径的一半 lineWidth等于期望半径 ...
- C#多线程:深入了解线程同步lock,Monitor,Mutex,同步事件和等待句柄(中)
本篇继续介绍WaitHandler类及其子类 Mutex,ManualResetEvent,AutoResetEvent的用法..NET中线程同步的方式多的让人看了眼花缭乱,究竟该怎么去理解呢?其实, ...
- ramfs/tmpfs: Linux RAM filesystems
Previously in windows, I uses ramdisk, to accelerate chrome browsing. However, in linux, this is mad ...
- iOS CFStringTransForm汉字转拼音
CFStringTransform - (NSString *) phonetic:(NSString*)sourceString { NSMutableString *source = [sourc ...