inheritance如何为属性工作?
属性上的Inherited bool属性是指什么? 
 这是否意味着如果我使用属性AbcAtribute (具有Inherited = true )定义我的类,并且如果我从该类inheritance另一个类,那么派生类也将具有相同的属性应用于它? 
为了用代码示例阐明这个问题,想象下面的内容:
 [AttributeUsage(AttributeTargets.Class, Inherited = true)] public class Random: Attribute { /* attribute logic here */ } [Random] class Mother { } class Child : Mother { } 
  Child是否也应用了Random属性? 
当Inherited = true(这是默认值)时,意味着您正在创build的属性可以由该属性修饰的类的子类inheritance。
所以 – 如果你用[AttributeUsage(Inherited = true)]创buildMyUberAttribute
 [AttributeUsage (Inherited = True)] MyUberAttribute : Attribute { string _SpecialName; public string SpecialName { get { return _SpecialName; } set { _SpecialName = value; } } } 
然后使用属性通过装饰超级…
 [MyUberAttribute(SpecialName = "Bob")] class MySuperClass { public void DoInterestingStuf () { ... } } 
如果我们创build一个MySuperClass的子类,它将拥有这个属性…
 class MySubClass : MySuperClass { ... } 
然后实例化一个MySubClass的实例…
 MySubClass MySubClassInstance = new MySubClass(); 
然后testing,看它是否有属性…
MySubClassInstance <—现在具有“Bob”作为SpecialName值的MyUberAttribute。
是的,这正是它的意思。 属性
 [AttributeUsage(Inherited=true)] public class FooAttribute : System.Attribute { private string name; public FooAttribute(string name) { this.name = name; } public override string ToString() { return this.name; } } [Foo("hello")] public class BaseClass {} public class SubClass : BaseClass {} // outputs "hello" Console.WriteLine(typeof(SubClass).GetCustomAttributes().First());