属性上的Inherited
bool属性是指什么?继承对属性如何工作?
是否意味着如果我使用属性AbcAtribute
(即Inherited = true
)定义了我的类,并且如果我从该类继承另一个类,那么派生类也会将相同的属性应用于它?
要澄清一个代码示例这个问题,设想以下:
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class Random: Attribute
{ /* attribute logic here */ }
[Random]
class Mother
{ }
class Child : Mother
{ }
是否Child
也有适用于它的Random
属性?
当你问这个问题时,情况并非如此,而是今天[继承的属性的官方文档](https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited。 aspx)有一个精心设计的例子,它显示了继承类和覆盖方法的'Inherited = true'和'Inherited = false'之间的区别。 – 2017-08-28 20:31:38