C#中的Attribute定义及用法
1.Attribute定义
// 长记法
[ConditionalAttribute("LI")]
[ConditionalAttribute("NoBug")]
publicstaticvoidFun()
{Console.WriteLine("Created By Li, NoBug.");}
// 短记法
[Conditional("LI")]
[Conditional("NoBug")]
publicstaticvoidFun()
{Console.WriteLine("Created By Li, NoBug.");}
就像牡蛎天生就要吸附在礁石或船底上一样,Attribute 的实例一构造出来就必需“粘”在一个什么目标上。
Attribute 实例化的语法是相当怪异的,主要体现在以下三点:
1. 不使用new 操作符来产生实例,而是使用在方括号里调用构造函数来产生实例。
2. 方括号必需紧挨着放置在被附着目标的前面。
3. 因为方括号里空间有限,不能像使用new 那样先构造对象,然后再给对象的属性(Property)赋值。
因此,对Attribute 实例的属性赋值也在构造函数的圆括号里。
并且,Attribute 实例化时尤其要注意的是:
1. 构造函数的参数是一定要写。有几个就得写几个,因为你不写的话实例就无法构造出来。
2. 构造函数参数的顺序不能错。调用任何函数都不能改变参数的顺序,除非它有相应的重载(Overload)。因为这个顺序是固定的,有些书里称其为“定位参数”(意即“个数和位置固定的参数”)。
3. 对Attribute 实例的属性的赋值可有可无。反正它会有一个默认值,并且属性赋值的顺序不受限制。有些书里称属性赋值的参数为“具名参数”。
自定义Attribute 实例
在此,我们不使用.NET Framework 中的各种Attribute 系统特性,而是从头自定义一个全新的Attribute 类。
示例代码如下:
using System;
namespace Con_Attribute
{
classProgram3
{
staticvoidMain(string[] args)
{
//使用反射读取Attribute
System.Reflection.MemberInfo info =typeof(Student);//通过反射得到Student类的信息
Hobby hobbyAttr =(Hobby)Attribute.GetCustomAttribute(info,typeof(Hobby));
if(hobbyAttr !=null)
{
Console.WriteLine("类名:{0}", info.Name);
Console.WriteLine("兴趣类型:{0}", hobbyAttr.Type);
Console.WriteLine("兴趣指数:{0}", hobbyAttr.Level);
}
}
}
//注意:"Sports" 是给构造函数的赋值, Level = 5 是给属性的赋值。
[Hobby("Sports",Level=5)]
classStudent
{
[Hobby("Football")]
publicstring profession;
publicstringProfession
{
get {return profession;}
set{ profession = value;}
}
}
//建议取名:HobbyAttribute
classHobby:Attribute// 必须以System.Attribute 类为基类,规范的写法一般是HobbyAttribute,用的时候可以简写为[Hobby(“”)]
{
// 参数值为null的string 危险,所以必需在构造函数中赋值
publicHobby(string _type)// 定位参数
{
this.type = _type;
}
//兴趣类型
privatestring type;
publicstringType
{
get {return type;}
set{ type = value;}
}
//兴趣指数
privateint level;
publicintLevel
{
get {return level;}
set{ level = value;}
}
}
}
classPerson:Attribute{
privatestring _name;
privateint _age;
publicstringName{
set{this._name = value;}
get {returnthis._name;}
}
publicintAge{
set{this._age = value;}
get {returnthis._age;}
}
publicPerson(string s,int i){
this.Name= s;
this.Age= i;
}
}
[Person("spike",23)]
class test {
privateint aa;
publicstring ss;
}
Person per;
foreach(var item intypeof(test).GetCustomAttributes(true)){
per = item asPerson;//若item是自定义的Person类,则将Person类返回
if(per !=null){
Console.WriteLine(per.Name);
Console.WriteLine(per.Age);
}
}
Console.ReadKey();
C#中的Attribute定义及用法的更多相关文章
- C++中宏的定义与用法(现已被内联函数所代替)
在noip中,宏还是被经常采用,所以这里讲一下,C++中宏的定义与用法 第一种用法——配合条件编译:#define DEBUG 定义一个叫DEBUG的标识符.它应该与#ifdef或#ifndef配合使 ...
- 关于C# 中的Attribute 特性
关于C# 中的Attribute 特性 作者: 钢钢 来源: 博客园 发布时间: 2011-01-09 23:30 阅读: 13921 次 推荐: 12 原文链接 [收藏] 摘要:纠结地说 ...
- C#中的Attribute和Java中的Annotation
在之前的博客中介绍过C#的Attribute(特性),简单的说,特性主要就是利用反射技术,在运行期获取关注类的相关标注信息,然后利用这些标注信息对关注的类进行处理,最近因为工作的原因,需要看一下Jav ...
- jquery中attr()与prop()函数用法实例详解(附用法区别)
本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...
- SpringMVC中 -- @RequestMapping的作用及用法
一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.x ...
- C#中的Attribute详解(下)
原文地址:https://blog.csdn.net/xiaouncle/article/details/70229119 C#中的Attribute详解(下) 一.Attribute本质 从上篇里我 ...
- Unity中使用Attribute
Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...
- Linq中关键字的作用及用法
Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
随机推荐
- 问题2-:Syntax error on tokens, delete these tokens
出现原因:拷贝下来的代码缺少{左大括号 然后运行时run as 没有选到java application 是因为没有main方法 加个public static void main(String() ...
- xcode Could not launch "" ; has denied the launch request
xcode Could not launch "" ; “”“ has denied the launch request (注意,这种方式不能调试) 1.编辑scheme 2 ...
- nginx源代码分析--nginx进程间通信
Linux下的IPC非常多,nginx的进程都是有亲缘关系的进程,对于他们的通信我们选择TCP socket进行通信. TCP socket 用来做进程通信的优点有, 1.socket是文件描 ...
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- JavaScript扩展运算符(...)
对象的扩展运算符 扩展运算符是三个点(...).用于取出参数对象的所有可遍历属性,然后拷贝到当前对象之中. 如上图所示,新建了一个对象a,然后通过扩展运算符将其属性x,y一并拷贝到b对象中. 合并两个 ...
- golang iris下面的websocket
最近要做后台主动推送:(iris框架,封装的有wesocket,刚开始以为直接拿过来用,结果不是现在贴一下代码,写一下遇到的坑) func main() { app := iris.New() ...
- king枚举帮助类
可以方便的实现枚举 枚举 public enum DeptType { [Description("科室1")] Professional = , [Description(&qu ...
- 以SqlHelper为例论面向对象中封装的使用(续)
上文以SqlHelper为例说明了面向对象中封装的好处,但是上文只是简单封装,考虑下面代码的情况: public static Activate GetByCode(string code) { Li ...
- Cupid's Arrow[HDU1756]
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- js判断浏览器是android还是ios还是微信浏览器
第一种方法<script type="text/javascript"> //判断访问终端 var browser={ versions:function(){ var ...