在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的继承的更多相关文章

  1. JS中的原型继承机制

    转载 http://blog.csdn.net/niuyongjie/article/details/4810835 在学习JS的面向对象过程中,一直对constructor与prototype感到很 ...

  2. C#中是否可以继承String类

    C#中是否可以继承String类? 答:String类是sealed类故不可以继承. 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. 在下面的示例中,类 HoverTree ...

  3. 理解JavaScript中的原型继承(2)

    两年前在我学习JavaScript的时候我就写过两篇关于原型继承的博客: 理解JavaScript中原型继承 JavaScript中的原型继承 这两篇博客讲的都是原型的使用,其中一篇还有我学习时的错误 ...

  4. C#中事件的继承

    C#中的子类无法调用父类的事件,可以通过在父类中创建一个方法来调用父类的事件,而子类通过调用父类的方法来触发事件. class parent { protected string name; publ ...

  5. C++中的虚继承 & 重载隐藏覆盖的讨论

    虚继承这个东西用的真不多.估计也就是面试的时候会用到吧.. 可以看这篇文章:<关于C++中的虚拟继承的一些总结> 虚拟基类是为解决多重继承而出现的. 如:类D继承自类B1.B2,而类B1. ...

  6. python中使用多继承

    python中使用多继承,会涉及到查找顺序(MRO).重复调用(钻石继承,也叫菱形继承问题)等 MRO MRO即method resolution order,用于判断子类调用的属性来自于哪个父类.在 ...

  7. Django中的Model继承

    Django 中的 model 继承和 Python 中的类继承非常相似,只不过你要选择具体的实现方式:让父 model 拥有独立的数据库:还是让父 model 只包含基本的公共信息,而这些信息只能由 ...

  8. 《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)

    一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性.当一个类继承其他类时,继承类叫子类(subclass),被继承类叫超类(或父类,supercla ...

  9. javascript中的原型继承

    在Javascript面向对象编程中,原型继承不仅是一个重点也是一个不容易掌握的点.在本文中,我们将对Javascript中的原型继承进行一些探索. 基本形式 我们先来看下面一段代码: <cod ...

随机推荐

  1. highchat中的category编程object问题

    设置highchart时的category时我是动态赋值的形式category=cat;cat是['title','title']是X轴的坐标显示但当单击chat的图例时X轴变成了object: ca ...

  2. javac 错误: 编码GBK的不可映射字符

    在java代码中有中文注释,使用javac编译时,出现编码报错. 错误: 编码GBK的不可映射字符 问题原因: 在编译的时候,如果我们没有用-encoding参数指定我们的JAVA源程序的编码格式,则 ...

  3. asp.net中iframe页面用jQuery向父页面传值

    在asp.net页面有时一个页面会通过iframe嵌套另一个页面,下面的例子讲述的是被嵌套的iframe页面向父页传值的一种方式,用jQuery即可. iframe页面代码: <!DOCTYPE ...

  4. net异步线程获取返回值的三种方式

    方式一:endInvoke using System; using System.Collections.Generic; using System.Text; using System.Thread ...

  5. NSOperationQueue 多线程

    staticNSOperationQueue * queue; - (void)viewDidLoad { [superviewDidLoad]; queue = [[NSOperationQueue ...

  6. Docker镜像的获取与删除

    Docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker会尝试先从默认镜像仓库下载(默认使用Dicker Hub公共注册服务器中的仓库),用户也可以通过配置,使用自定义的镜像仓库 ...

  7. vc2010 win32 控制台应用程序中文乱码

    vc2010 win32 控制台应用程序中文乱码 在 vc2010 上用 win32 控制台程序写些测试代码调用 windows api ,处理错误信息时,发现用 wprintf 输出的错误信息出现了 ...

  8. 还原SQLServer2008数据库报用户无法登录 .

    在一台新的服务器上还原mssql2008r2数据库后,原来数据库中的账户无法用来打开这台新还原的数据库,报错:登录失败 错误代码:4064. 分析原因:在备份数据库的时候,服务器引擎中的安全-> ...

  9. Maven 命令操作项目

    1.创建一个多模块的Java项目 shift+鼠标右键 创建项目命令: 旧版: mvn archetype:create -DgroupId=com.qhong -DartifactId=MavenP ...

  10. Vue.2.0.5-插件

    开发插件 插件通常会为Vue添加全局功能.插件的范围没有限制--一般有下面几种: 添加全局方法或者属性,如: vue-element 添加全局资源:指令/过滤器/过渡等,如 vue-touch 通过全 ...