阅读目录 
   一: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. Hibernate配置文件与映射文件的创建

    1. config文件的创建: 内容: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hib ...

  2. 【转载】-- vi/vim使用

    vi/vim 基本使用方法本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...

  3. codeforces 425D

    题意:给定n<=100000个二维点,并且0<=x,y<=100000,求有多少个平行于坐标轴的正方形 思路:本来想hash的,但是感觉不好弄.. 后来感觉像是分块,最坏的情况就是那 ...

  4. Linux下which、whereis、locate、find命令的区别

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料(参考资料1),因为有时很长时间不会用到,当要用的时候经常弄混了,所以放到这里方便使用. w ...

  5. ubuntn下 nginx+phpstorm 中配置xdebug调试

    xdebug安装和配置说明,主要用于个人学习记录. 一.echo phpinfo(); 搜素xdebug,若未搜素到,则标识未安装或安装失败. 二.拷贝步骤1中输出的所有结果.访问http://xde ...

  6. 用groovy采集网页数据

    首先,用 http://groovyconsole.appspot.com/ 测试下面的代码,发现引用总是失败. 下载了GGTS: https://spring.io/tools/ggts 测试成功: ...

  7. 配置Linux自动挂载

    使用mount命令来挂载硬件,在Linux重启后这些挂载信息会丢失,因此对应磁盘这类硬件,需要配置自动挂载来保证系统重启时进行自动挂载. 自动挂载信息保存在文件/etc/fstab文件中 查看该文件的 ...

  8. NBIbatis web/winform框架

    Web框架 调用Bussiness和DataAccess可参考微信框架的后台. Pages/Meeting/MeetingList.aspx Pages/Meeting/MeetingEdit.asp ...

  9. SQL Server 2014 安装小记

    一.写在前面 由于想体验下微软的Windows Azure在SQL Server数据库方面的使用,笔者花了点时间安装了一下SQL Server 2014,安装很简单,基本就是稍微做些配置即可,笔者在此 ...

  10. 最新QQ强制聊天代码,同时可判断好友关系

    QQ强聊虽然早就变成了一个传说,但现在依然可以实现. 小菜其实早就知道这个漏洞,但是一直没公布,前两天突然来兴致试了试,没想到漏洞依然存在. 然后小菜跑到了乌云漏洞报告平台举报漏洞,但没想到被腾讯鲁莽 ...