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 ...
随机推荐
- Android的项目不能直接引用可移植类库的项目解决方法
深圳_exception() 10:25:51 Android的项目不能直接引用可移植类库的项目,但是可以引用可移植类库生成的dll,这就意味着无法直接断电调试可移植类库上海-黄药师() 10:26: ...
- delphi平方开方
用delphi自带的sqr和sqrt函数 procedure TForm2.SpeedButton3Click(Sender: TObject); begin ShowMessage(Sqr(6).T ...
- 一个通用的makefile
# ESDK the makefile setting file - chenwg@20131014 # you can modify "PC = 1" such as " ...
- SQL Server 内存数据库原理解析
前言 关系型数据库发展至今,细节上以做足文章,在寻求自身突破发展的过程中,内存与分布式数据库是当下最流行的主题,这与性能及扩展性在大数据时代的需求交相辉映.SQL Server作为传统的数据库也在最新 ...
- Orchard 精简版
Orchard Express v1.7.2 精简版 保留Orchard.Framework和Orchard.Core全部源码(一字未改),去除非必要模块(仅剩Orchard.jQuery, Orch ...
- Windows 10 技术预览
windows10的技术预览版已经发布了很久了,正式版大约在今年的夏天就会发布,作为微软寄予厚望的下一代全平台操作系统,相比于windows8.1,windows10做了哪些改进,又添加了哪些新功能. ...
- 由于外键的存在引发的一个mysql问题 Cannot change column 'id': used in a foreign key constraint
Duplicate entry ' for key 'PRIMARY' 一查,发现表没有设置自增长. 尝试增加修改表,添加自增长. ALTER TABLE sh_incentive_item MODI ...
- [Java拾遗二]Tomact及Http 部分总结.
前言: 刚好今天回来的很早, 总结下 Tomcat及Http的基础知识. 1, Tomcat web相关概念 web:网页的意思,网页资源包括服务器上的所有资源. ...
- js勾选时显示相应内容
使用环境,一.比如用户勾选时显示一些安全方面提示“你真的要自动登录吗?这将使你下次不需要密码即可进入你的个人中心.”二.显示其他预设选项,以方便用户选择输入,比如密保问题设置,当用户不想使用自定义设置 ...
- Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系
Atitit.如何实现dip, di ,ioc ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的 ...