一、创建属性

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor, AllowMultiple = true, Inherited = true)]
//AttributeTargets:属性应用到的目标类型。AllowMultiple:是否允许一个元素应用多个此属性。Inherited:属性能否有派生类继承。
public class CodeStatusAttribute : Attribute
{
private string status;
public CodeStatusAttribute(string status)//构造函数为位置参数
{
this.status = status;
}
public string Tester { set; get; }//属性和公共字段为命名参数
public string Coder { set; get; } public override string ToString()
{
return status;
}
}

二、应用属性

//1、使用单个属性
[CodeStatus("a版")]
public class Tringe
{ } //2、使用多个属性
[CodeStatus("b版", Coder = "小李")]
[CodeStatus("b版", Coder = "小王")]
//也可以[CodeStatus("aa",Coder="小李"),CodeStatus("aa",Coder="小王")]
public class Square
{ } //3、使用位置参数和命名参数
//type表示此属性与什么元素关联,可能有:assembly,field,method,param,property,return,moudule,event,type等。。
[type: CodeStatus("最终版", Coder = "小李", Tester = "老李")]
public class Circle
{
[CodeStatus("最终版", Coder = "小李", Tester = "老李")]
public Circle()
{ }
}

三、反射属性

//1、获取类上的属性。
Type t = typeof(Circle);
Attribute[] attArr = Attribute.GetCustomAttributes(t, typeof(CodeStatusAttribute));
//或
object[] attArr1 = t.GetCustomAttributes(typeof(CodeStatusAttribute), true); //2、获取成员上属性
Attribute[] attArr3 = t.GetConstructors()[0].GetCustomAttributes().ToArray();//构造函数,获取字段GetField("..") //3、遍历
foreach (Attribute attr in attArr3)
{
CodeStatusAttribute item = (CodeStatusAttribute)attr;
Console.Write(item.ToString() + item.Coder + item.Tester);
}

四、Net内置属性

[Condeitonal] //条件控制
[Obsolete] //废弃属性
[Serializable]//可序列化属性
[AssemblyDelaySign] //程序集延迟签名

属性 Attribute的更多相关文章

  1. C#属性(Attribute)用法实例解析

    属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变.本文就以实例形式分析了C#中属性的应用.具体入戏: 一.运用范围 程序集,模块,类型(类,结构,枚举,接口,委 ...

  2. Android应用资源--之属性(Attribute)资源

    原文链接: http://wujiandong.iteye.com/blog/1184921 属性(Attribute)资源:属于整个Android应用资源的一部分.其实就是网上一堆介绍怎么给自定义V ...

  3. opencart3属性attribute实现换行等简单html代码

    opencart3属性attribute在前台页面默认是没有解析html代码功能的,比如想实现换行,后台这样写:line 1<br>line 2,但前台产品页也是line 1<br& ...

  4. Servlet中的属性(attribute)和参数(parameter)的区别

    1.引子 初学者对属性(attribute)和参数(parameter)容易搞混.没搞清他们的区别,项目中就可能出现一此莫名其妙的问题. 2.两者的区别 1) 属性(attribute) 属性是在后台 ...

  5. C#教程之C#属性(Attribute)用法实例解析

    引用:https://www.xin3721.com/ArticlecSharp/c11686.html 属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变.本文 ...

  6. 转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨

    1.引子 转发和重定向是我们在做web项目中常用到的两个术语,有必要理清两者的区别和与之相关的参数.属性获取问题. 2.转发和重定向 1).转发 转发是服务器行为,将当前请求(Request)和响应( ...

  7. boolean attribute(布尔值属性) attribute vs property

    boolean attribute(布尔值属性) boolean attribute     HTML - Why boolean attributes do not have boolean val ...

  8. 属性attribute和property的区别

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  9. 蓝牙BLE: ATT协议层中属性(Attribute)

    ATT(Attribute Protocol)属性层是GATT和GAP的基础,它定义了BLE协议栈上层的数据结构和组织方式. 属性(Attribute)概念是ATT层的核心,ATT层定义了属性的内容, ...

随机推荐

  1. C++比起C来新增的拓展

    命名空间 register 在C语言横行的时代,为了加快运行速度,一些关键变量会被放入寄存器中,程序代码请求编译器把变量存入寄存器,然而C语言版的寄存器变量无法通过地址获得register变量.c++ ...

  2. Nachos java版学习(二)

    threads.Lock类 提 供 了 锁 以 保 证 互 斥. 在 临 界 代 码 区 的 两 端 执 行 Lock.acquire()和Lock.release()即可保证同时只有一个线程访问临界 ...

  3. [转帖]为何 CPU 只用硅,而不用能耗更低的锗制作?知乎好文章

    作者:鲁超链接:https://www.zhihu.com/question/28935966/answer/617701106来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  4. TypeScript 高级类型

    ⒈交叉类型(Intersection Types) 交叉类型是将多个类型合并为一个类型. 这让我们可以把现有的多种类型叠加到一起成为一种类型,它包含了所需的所有类型的特性. 例如, Person &a ...

  5. eclipse3.62开发第一个java程序HelloWorld

    [学习笔记] 用eclipse3.62开发第一个java程序: 使用eclipse之前们需要先配置一下jdk.window/preference/java/installed JREs,详细请见视频. ...

  6. sonar+Jenkins 构建代码质量自动化分析平台

    1.Sonar 介绍 Sonar 是一个用于管理代码质量的开源工具,可以分析代码中的bug和漏洞以及Code Smells,支持20多种编程语言的检测,如java,c/c++,python,php等语 ...

  7. (九) spring 使用自定义限定符注解

    案例一 定义接口  CD.java package interfacepackage; public interface CD { void play(); } 定义接口 player .java p ...

  8. 数据格式转换string.Format

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  9. putty和psftp命令行参数

    putty和psftp命令行参数 https://the.earth.li/~sgtatham/putty/latest/w32/putty.zip https://the.earth.li/~sgt ...

  10. Eclipse 设置代码模板

    checkstyle插件 安装 从 http://sourceforge.jp/projects/sfnet_eclipse-cs/releases/ 下载 net.sf.eclipsecs-upda ...