<bdo id='DmUHT'></bdo><ul id='DmUHT'></ul>
<i id='DmUHT'><tr id='DmUHT'><dt id='DmUHT'><q id='DmUHT'><span id='DmUHT'><b id='DmUHT'><form id='DmUHT'><ins id='DmUHT'></ins><ul id='DmUHT'></ul><sub id='DmUHT'></sub></form><legend id='DmUHT'></legend><bdo id='DmUHT'><pre id='DmUHT'><center id='DmUHT'></center></pre></bdo></b><th id='DmUHT'></th></span></q></dt></tr></i><div id='DmUHT'><tfoot id='DmUHT'></tfoot><dl id='DmUHT'><fieldset id='DmUHT'></fieldset></dl></div>

<tfoot id='DmUHT'></tfoot>

      1. <legend id='DmUHT'><style id='DmUHT'><dir id='DmUHT'><q id='DmUHT'></q></dir></style></legend>

        <small id='DmUHT'></small><noframes id='DmUHT'>

      2. 继承如何对属性起作用?

        时间:2023-07-27
        <tfoot id='db7wq'></tfoot>
            <tbody id='db7wq'></tbody>
          • <i id='db7wq'><tr id='db7wq'><dt id='db7wq'><q id='db7wq'><span id='db7wq'><b id='db7wq'><form id='db7wq'><ins id='db7wq'></ins><ul id='db7wq'></ul><sub id='db7wq'></sub></form><legend id='db7wq'></legend><bdo id='db7wq'><pre id='db7wq'><center id='db7wq'></center></pre></bdo></b><th id='db7wq'></th></span></q></dt></tr></i><div id='db7wq'><tfoot id='db7wq'></tfoot><dl id='db7wq'><fieldset id='db7wq'></fieldset></dl></div>

              <legend id='db7wq'><style id='db7wq'><dir id='db7wq'><q id='db7wq'></q></dir></style></legend>

                • <bdo id='db7wq'></bdo><ul id='db7wq'></ul>

                  <small id='db7wq'></small><noframes id='db7wq'>

                • 本文介绍了继承如何对属性起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  属性上的 Inherited bool 属性指的是什么?

                  What does the Inherited bool property on attributes refers to?

                  这是否意味着如果我使用属性 AbcAtribute(具有 Inherited = true)定义我的类,并且如果我从该类继承另一个类,那么派生类也会应用相同的属性吗?

                  Does it mean that if I define my class with an attribute AbcAtribute (that has Inherited = true), and if I inherit another class from that class, that the derived class will also have that same attribute applied to it?

                  为了用一个代码示例来澄清这个问题,想象一下:

                  To clarify this question with a code example, imagine the following:

                  [AttributeUsage(AttributeTargets.Class, Inherited = true)]
                  public class Random: Attribute
                  { /* attribute logic here */ }
                  
                  [Random]
                  class Mother 
                  { }
                  
                  class Child : Mother 
                  { }
                  

                  Child 是否也应用了 Random 属性?

                  Does Child also have the Random attribute applied to it?

                  推荐答案

                  当Inherited = true(默认)时,表示你创建的属性可以被该属性修饰的类的子类继承.

                  When Inherited = true (which is the default) it means that the attribute you are creating can be inherited by sub-classes of the class decorated by the attribute.

                  所以 - 如果您使用 [AttributeUsage (Inherited = true)] 创建 MyUberAttribute

                  So - if you create MyUberAttribute with [AttributeUsage (Inherited = true)]

                  [AttributeUsage (Inherited = True)]
                  MyUberAttribute : Attribute
                  {
                     string _SpecialName;
                     public string SpecialName
                     { 
                       get { return _SpecialName; }
                       set { _SpecialName = value; }
                     }
                  }
                  

                  然后通过装饰超类来使用属性...

                  Then use the Attribute by decorating a super-class...

                  [MyUberAttribute(SpecialName = "Bob")]
                  class MySuperClass 
                  {
                    public void DoInterestingStuf () { ... }
                  }
                  

                  如果我们创建 MySuperClass 的子类,它将具有此属性...

                  If we create an sub-class of MySuperClass it will have this attribute...

                  class MySubClass : MySuperClass
                  {
                     ...
                  }
                  

                  然后实例化一个 MySubClass 的实例...

                  Then instantiate an instance of MySubClass...

                  MySubClass MySubClassInstance = new MySubClass();
                  

                  然后测试一下是否有属性...

                  Then test to see if it has the attribute...

                  MySubClassInstance <--- 现在拥有 MyUberAttribute,其中Bob"作为 SpecialName 值.

                  MySubClassInstance <--- now has the MyUberAttribute with "Bob" as the SpecialName value.

                  这篇关于继承如何对属性起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:读取方法的属性值 下一篇:使用 Serializable 属性 &amp; 有什么区别?实现 ISerializable?

                  相关文章

                  1. <small id='2VE3L'></small><noframes id='2VE3L'>

                    <legend id='2VE3L'><style id='2VE3L'><dir id='2VE3L'><q id='2VE3L'></q></dir></style></legend>

                    <tfoot id='2VE3L'></tfoot>

                      • <bdo id='2VE3L'></bdo><ul id='2VE3L'></ul>

                      <i id='2VE3L'><tr id='2VE3L'><dt id='2VE3L'><q id='2VE3L'><span id='2VE3L'><b id='2VE3L'><form id='2VE3L'><ins id='2VE3L'></ins><ul id='2VE3L'></ul><sub id='2VE3L'></sub></form><legend id='2VE3L'></legend><bdo id='2VE3L'><pre id='2VE3L'><center id='2VE3L'></center></pre></bdo></b><th id='2VE3L'></th></span></q></dt></tr></i><div id='2VE3L'><tfoot id='2VE3L'></tfoot><dl id='2VE3L'><fieldset id='2VE3L'></fieldset></dl></div>