阅读目录 
   一:C#自定义Attribute

   二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets
   三:AttributeUsageAttribute中的3个属性(Property)中的,AllowMultiple
   四:AttributeUsageAttribute中的3个属性(Property)中的Inherited

 一:C#自定义Attribute
  
写一个类继承自System.Attribute就可以了
  二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets
     
这个属性类能应用于哪种类型上面,如下图,我的例子中声明属性类HelperAttribute只能用于interface接口上面,而我实际应用于class上面编译就报错了说声明类型不对
 
  
  

 三:AttributeUsageAttribute中的3个属性(Property)中的,AllowMultiple
   能否多次声明指定的属性,如下图AllowMultiple=false的话,我在一个类上声明2次,编辑就报错了

四:AttributeUsageAttribute中的3个属性(Property)中的Inherited
我们在父类上声明属性类,而在子类上不声明属性类,把属性类设置为Inherited = false,我们看到查找SubMyClass1类型没有找到它的父类MyClass1的HelperAttribute属性类,所以没有任何输出

我们改为Inherited = true后,再调试看到查找SubMyClass1类型找到了它父类的HelperAttribute属性类,然后输出描述

我们在父类上声明属性类,也在子类上声明属性类,把属性类设置为 AllowMultiple = false, Inherited = true,我们看到查找SubMyClass1类型找到了它自己的HelperAttribute属性类,然后输出描述,没有找到父类MyClass1的HelperAttribute属性类,是因为 AllowMultiple = false不允许多重属性,所以父类的HelperAttribute属性类被子类SubMyClass1的HelperAttribute属性类覆盖了,所以最终只能找到子类的属性类

我们再改为AllowMultiple = true, Inherited = true,我们看到查找SubMyClass1类型不但是找到了它自己的HelperAttribute属性类而且找到了父类MyClass1的HelperAttribute属性类,所以最终会输出两条信息

实例代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace CustomizeAttribute
{
class Program
{
static void Main(string[] args)
{
foreach (Attribute attr in typeof(SubMyClass1).GetCustomAttributes(true))
{
HelperAttribute helperAtt = attr as HelperAttribute;
if (helperAtt != null)
{
Console.WriteLine("Name:{0} Description:{1}",helperAtt.Name, helperAtt.Description);
}
} Console.ReadLine();
}
} [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class HelperAttribute : System.Attribute
{
private string _name;
private string _description; public HelperAttribute(string des)
{
this._name = "Default name";
this._description = des;
} public HelperAttribute(string des1,string des2)
{
this._description = des1;
this._description = des2;
} public string Description
{
get
{
return this._description;
}
set
{
this._description = value;
}
} public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
} public string PrintMessage()
{
return "PrintMessage";
}
} [HelperAttribute("this is my class1")]
public class MyClass1
{ } public class SubMyClass1 : MyClass1
{ } [HelperAttribute("this is my class2", Name = "myclass2")]
public class MyClass2
{ } [HelperAttribute("this is my class3", Name = "myclass3", Description = "New Description")]
public class MyClass3
{ } [HelperAttribute("this is my class4", "this is my class4")]
public class MyClass4
{ }
}

2.C#自定义Attribute的更多相关文章

  1. XsdGen:通过自定义Attribute与反射自动生成XSD

    前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...

  2. 自定义Attribute 服务端校验 客户端校验

    MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...

  3. C#自定义Attribute值的获取与优化

    C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...

  4. .net c#获取自定义Attribute

    前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) ...

  5. CVS导出&&自定义Attribute的使用

    1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...

  6. 转:C#制作ORM映射学习笔记一 自定义Attribute类

    之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...

  7. 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻

    原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...

  8. 通过自定义Attribute及泛型extension封装数据验证过程

    需求来源: 在日常工作中,业务流程往往就是大量持续的数据流转.加工.展现过程,在这个过程中,不可避免的就是数据验证的工作.数据验证工作是个很枯燥的重复劳动,没有什么技术含量,需要的就是对业务流程的准确 ...

  9. 自定义Attribute类

    在我们的项目中有时经常会标识一些类的特性,在下面我们将简短的方式来介绍如何构建自定义的Attribute类. using System; using System.Collections.Generi ...

随机推荐

  1. (并查集)~APTX4869(fzu 2233)

    http://acm.fzu.edu.cn/problem.php?pid=2233 Problem Description 为了帮助柯南回到一米七四,阿笠博士夜以继日地研究APTX4869的解药.他 ...

  2. 【原创】-- nfs安装配置及使用,挂载根文件系统

    环境:ubuntu14.04(非虚拟机),OK6410 环境搭建: (1) #sudo apt-get install nfs-kernel-server 如果已经是最新版本了,无需安装 (2) 建立 ...

  3. Android开发笔记

    Android 中国SDK: http://wear.techbrood.com/ Android SDK Manager 代理设置: http://www.cnblogs.com/sunzn/p/4 ...

  4. Linux: shell常用通配符

    字符 含义 * 匹配 0 或多个字符 ? 匹配任意一个字符 [list] 匹配 list 中的任意单一字符 [!list] 匹配 除list 中的任意单一字符以外的字符 [c1-c2] 匹配 c1-c ...

  5. AVL树(三)之 Java的实现

    概要 前面分别介绍了AVL树"C语言版本"和"C++版本",本章介绍AVL树的Java实现版本,它的算法与C语言和C++版本一样.内容包括:1. AVL树的介绍 ...

  6. Win10年度更新开发必备:VS2015 Update 3正式版下载汇总

    微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...

  7. paip.批处理清理java项目冗余jar的方法

    paip.批处理清理java项目冗余jar的方法 在myeclipse中开发的java项目遇到jar包冗余情况,如何删除项目中的冗余jar包啊?项目很大jar包一百多个. 2010-09-14 14: ...

  8. JQuery快速入门

    Write less, do more, I like jQuery. jQuery是最常用的js库,整体来说非常轻量并易于扩展,对于移动应用可以使用其更轻量的孪生兄弟Zepto代替.其是由John ...

  9. 炜煌T3POS58微打参数设置方法

     本文用菊子曰发布

  10. C#程序中注释过多的8条理由

    程序中中的注释,一般是有益处的,可以知晓程序的一些逻辑说明,或是参数解释.但是有些程序,因为注释太多,反而引起维护上的不方便,删掉了怕以后不能出现问题不好查找原因,不删除留在代码中,对程序的维护人员, ...