2.C#自定义Attribute
阅读目录
一: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的更多相关文章
- XsdGen:通过自定义Attribute与反射自动生成XSD
前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...
- 自定义Attribute 服务端校验 客户端校验
MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...
- C#自定义Attribute值的获取与优化
C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...
- .net c#获取自定义Attribute
前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) ...
- CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...
- 转:C#制作ORM映射学习笔记一 自定义Attribute类
之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...
- 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻
原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
- 通过自定义Attribute及泛型extension封装数据验证过程
需求来源: 在日常工作中,业务流程往往就是大量持续的数据流转.加工.展现过程,在这个过程中,不可避免的就是数据验证的工作.数据验证工作是个很枯燥的重复劳动,没有什么技术含量,需要的就是对业务流程的准确 ...
- 自定义Attribute类
在我们的项目中有时经常会标识一些类的特性,在下面我们将简短的方式来介绍如何构建自定义的Attribute类. using System; using System.Collections.Generi ...
随机推荐
- MyEclipse 10 中如何更改字体
打开Myeclipse软件.window->preferences->General->Appearance->Colors and Fonts然后在窗口的右边会显示一些如下图 ...
- JS检测浏览器是否支持HTML5视频播放 (标签<video>) ,
function checkVideo() { if (!!document.createElement('video').canPlayType) { var vidTest = document. ...
- 人脸识别经典算法一:特征脸方法(Eigenface)
这篇文章是撸主要介绍人脸识别经典方法的第一篇,后续会有其他方法更新.特征脸方法基本是将人脸识别推向真正可用的第一种方法,了解一下还是很有必要的.特征脸用到的理论基础PCA在另一篇博客里:特征脸(Eig ...
- PHP字符处理基础知识
<?php class StrDemo { function StrTest() { $s = "abcd"; print '$s length:'.strlen($s).& ...
- Asp.Net Web API 2第十二课——Media Formatters媒体格式化器
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本教程演示如何在ASP.N ...
- js 合并数组
<script type="text/javascript"> var a = '[{"name":"aaa& ...
- 抢小米软件html版(简单有效)
地球人都知道小米性价比高,大家都很期待,但是抢小米却是一件很头疼的事. 本来抢的人就多,还有一些大牛.黄牛使用软件来抢,人家有成百上千台电脑,开上几万个线程,很难抢过人家... 小菜分享一款简单的小米 ...
- 最新 Windows 10 应用项目模板发布
以下是最新的Visual Studio 2015 Windows 10 应用程序模板. Windows 10中几乎所有的官方应用都遵循这样一个设计模板:在左上方有一个所谓的导航栏.点击该导航按钮,左侧 ...
- C++数组和指针
<C++ Primer 4th>读书摘要 与 vector 类型相似,数组也可以保存某种类型的一组对象:而它们的区别在于,数组的长度是固定的.数组一经创建,就不允许添加新的元素.指针则可以 ...
- C#与数据库访问技术总结(六)之Command对象创建SQl语句代码示例
Command对象创建SQl语句代码示例 说明:前面介绍了 Command 对象的方法和一些属性,回顾一下 Command对象主要用来执行SQL语句.利用Command对象,可以查询数据和修改数据. ...