C#中Attribute的继承
在C#中Attribute是个非常有用的语法,本文不会介绍Attribute的使用方法,如果想了解Attribute的详细信息请查阅MSDN及网上相关文档。C#中的Attribute有两个地方是和继承相关的,一个地方是AttributeUsageAttribute类中的属性参数Inherited,另一个地方是Type反射中的GetCustomAttributes方法(包括Type.GetCustomAttributes、MemberInfo.GetCustomAttributes等反射方法)的inherit参数,这两个与继承相关的参数都是bool类型,但是代表的含义却不相同,我们来看看他们分别代表的是什么:
AttributeUsageAttribute类的Inherited属性参数
AttributeUsage中的属性参数Inherited指的是继承链中的子类和子类成员是否能继承在父类和父类成员上声明的Attribute。我们来看个例子,假如现在我们有两个类BaseClass和MyClass,其中MyClass继承于BaseClass,现在我们在BaseClass上声明一个自定义Attribute叫MyAttribute,代码如下:
[My]
class BaseClass
{ } class MyClass:BaseClass
{ }
如果像下面代码中设置MyAttribute上AttributeUsage的属性参数Inherited为true,则代表MyAttribute是可以被继承声明的,也就是说上面代码中在父类BaseClass上声明了MyAttribute后,子类MyClass相当于也声明了MyAttribute。
[AttributeUsage(AttributeTargets.All, Inherited = true)]
class MyAttribute : Attribute
{
}
如果像下面代码中设置MyAttribute上AttributeUsage的属性参数Inherited为false,那么代表MyAttribute是无法被继承声明的,也就是说上面代码中在父类BaseClass上声明了MyAttribute后,子类MyClass不具有MyAttribute,相当于父类BaseClass声明了MyAttribute但是子类MyClass没有声明MyAttribute。
[AttributeUsage(AttributeTargets.All, Inherited = false)]
class MyAttribute : Attribute
{
}
GetCustomAttributes方法的inherit参数
GetCustomAttributes方法中inherit参数指的是当调用GetCustomAttributes方法时,GetCustomAttributes方法是否搜索子类和子类成员上从父类继承的Attribute。同样我们来看个例子,假如现在我们有两个类BaseClass和MyClass,其中MyClass继承于BaseClass,现在我们在BaseClass上声明一个自定义Attribute叫MyAttribute,代码如下:
[My]
class BaseClass
{ } class MyClass:BaseClass
{ }
我们在MyAttribute上设置AttributeUsage的属性参数Inherited为true,那么相当于子类MyClass上也声明了MyAttribute:
[AttributeUsage(AttributeTargets.All, Inherited = true)]
class MyAttribute : Attribute
{
}
现在如果我们调用构造函数构造一个MyClass对象,然后调用对象的反射方法GetCustomAttributes看是否能在MyClass上找到MyAttribute。如果找到了则在控制台输出找到了MyAttribute,否则输出没有找到。
首先我们调用GetCustomAttributes方法时将参数inherit设置为true。结果控制台显示:"MyAttribute found!",说明GetCustomAttributes方法成功找到了MyAttribute。
var my = new MyClass();
var myMyAttribute = my.GetType().GetCustomAttributes(typeof(MyAttribute), true); if (myMyAttribute != null && myMyAttribute.Count() > )
{
Console.WriteLine("MyAttribute found!");
}
else
{
Console.WriteLine("MyAttribute not found!");
}
接着我们调用GetCustomAttributes方法时将参数inherit设置为false。这次控制台显示:"MyAttribute not found!",说明将参数inherit设置为false后GetCustomAttributes方法无法在MyClass上找到MyAttribute了。
var my = new MyClass();
var myMyAttribute = my.GetType().GetCustomAttributes(typeof(MyAttribute), false); if (myMyAttribute != null && myMyAttribute.Count() > )
{
Console.WriteLine("MyAttribute found!");
}
else
{
Console.WriteLine("MyAttribute not found!");
}
这说明GetCustomAttributes方法的inherit参数实际上就是在指示是否去搜索从父类继承来的Attribute,由于本例中的MyAttribute是声明在父类BaseClass上的,所以当在子类MyClass的反射中调用GetCustomAttributes(typeof(MyAttribute), false)时,GetCustomAttributes方法将不会去搜索从父类继承的Attribute,所以在控制台上显示MyClass上无法找到MyAttribute。但是如果在子类MyClass的反射中调用GetCustomAttributes(typeof(MyAttribute), true),那么GetCustomAttributes方法不光会搜索子类MyClass自己声明的Attribute还会去搜索从父类BaseClass继承的Attribute,由于父类BaseClass上声明了MyAttribute,所以GetCustomAttributes方法在子类MyClass上也能找到MyAttribute,最后在控制台上显示在MyClass上找到了MyAttribute。如果将MyAttribute声明在子类MyClass上而不是像本例代码一样声明在父类BaseClass上,那么在调用GetCustomAttributes(typeof(MyAttribute), false)时,就会显示在MyClass上找到MyAttribute了。
总结一下,我们可以看到AttributeUsageAttribute类的Inherited属性参数和GetCustomAttributes方法的inherit参数代表的含义是不一样的。AttributeUsageAttribute类的Inherited属性代表的是父类或父类成员上声明的Attribute是否能够被应用到继承链中子类或子类成员上去。而GetCustomAttributes方法的inherit参数代表的是调用GetCustomAttributes方法时是否去搜索继承链中从父类或父类成员继承下来的Attribute。
C#中Attribute的继承的更多相关文章
- JS中的原型继承机制
转载 http://blog.csdn.net/niuyongjie/article/details/4810835 在学习JS的面向对象过程中,一直对constructor与prototype感到很 ...
- C#中是否可以继承String类
C#中是否可以继承String类? 答:String类是sealed类故不可以继承. 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. 在下面的示例中,类 HoverTree ...
- 理解JavaScript中的原型继承(2)
两年前在我学习JavaScript的时候我就写过两篇关于原型继承的博客: 理解JavaScript中原型继承 JavaScript中的原型继承 这两篇博客讲的都是原型的使用,其中一篇还有我学习时的错误 ...
- C#中事件的继承
C#中的子类无法调用父类的事件,可以通过在父类中创建一个方法来调用父类的事件,而子类通过调用父类的方法来触发事件. class parent { protected string name; publ ...
- C++中的虚继承 & 重载隐藏覆盖的讨论
虚继承这个东西用的真不多.估计也就是面试的时候会用到吧.. 可以看这篇文章:<关于C++中的虚拟继承的一些总结> 虚拟基类是为解决多重继承而出现的. 如:类D继承自类B1.B2,而类B1. ...
- python中使用多继承
python中使用多继承,会涉及到查找顺序(MRO).重复调用(钻石继承,也叫菱形继承问题)等 MRO MRO即method resolution order,用于判断子类调用的属性来自于哪个父类.在 ...
- Django中的Model继承
Django 中的 model 继承和 Python 中的类继承非常相似,只不过你要选择具体的实现方式:让父 model 拥有独立的数据库:还是让父 model 只包含基本的公共信息,而这些信息只能由 ...
- 《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)
一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性.当一个类继承其他类时,继承类叫子类(subclass),被继承类叫超类(或父类,supercla ...
- javascript中的原型继承
在Javascript面向对象编程中,原型继承不仅是一个重点也是一个不容易掌握的点.在本文中,我们将对Javascript中的原型继承进行一些探索. 基本形式 我们先来看下面一段代码: <cod ...
随机推荐
- Linux-modules software
简介 这里指的modules不是linux内核相关的module,只是用于软件多版本控制的一个开源软件包,比如说系统同时有neo4j的不同版本,使用modules软件就可以使得在需要的时候选择相应的软 ...
- magento 图片缓存是如何生成的
Varien_Image_Adapter_Gd2 类 ,里面有个save()方法,通过图片格式拼出来的方法 call_user_func_array($this->_getCallback(' ...
- Weak Pair---hud5877大连网选(线段树优化+dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...
- Swift-06-闭包
看完记不住,只好继续抄课文. 如果某个存储型属性的默认值需要特别的定制或者准备,就可以使用闭包或者全局函数来为其属性提供定制的默认值.每当某个属性所属的新类型实例创建时,对应的闭包或者函数会被调用,而 ...
- Medical image computing
Processing and analysis of medical images using computer comprises the following: image formation an ...
- Android 动态添加删除ExpandableListView的item的例子
这个例子可以学习到如下几点: 1.通过自定义Dialog(单独布局的xml文件进行弹出显示) 2.通过menu点击监听添加,删除view中的items 3.点击ExpandableListView中g ...
- APICloud全面支持WiFi真机同步和实时预览功能
APICloud工具插件包括APICloud Studio.Sublime Text和Webstorm全面为开发者提供iOS和Android平台真机同步调试功能,不仅可以通过USB方式进行APP真机同 ...
- WPF--Blend制作Button控件模板--问题补充
补充记录Button控件模板 控件模板制作过程中出现下图问题:动画对象不能用于动画属性"Fill” 并且这类问题Blend4中包括VS2010中仍然可以运行,但是只有VS2010中会报错:如 ...
- java中的、标识符、运算符以及数据类型之间的转换。
---恢复内容开始--- 数据类型之间的转换: 1:自动转换:就是不用说出要转换成什么类型,由java中的虚拟机自动将小数据类型转换成大数据类型,但大数据中的数据精度有可能被破坏. 2:强制转换:强制 ...
- python ftplib,smtplib,poplib学习
一.ftplib from ftplib import FTP ftpobj = FTP(IP或域名) #实例化对象 ftpobj.login('username','passwd') ## ...