C# 属性如何继承?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1240960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:04:52  来源:igfitidea点击:

How does inheritance work for Attributes?

c#.netvb.netattributes

提问by devoured elysium

What does the Inheritedbool property on attributes refers to?

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

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?

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

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 
{ }

Does Childalso have the Randomattribute applied to it?

是否ChildRandom应用了该属性?

采纳答案by cmdematos.com

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.

当 Inherited = true (这是默认值)时,这意味着您正在创建的属性可以被该属性修饰的类的子类继承。

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

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

[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 () { ... }
}

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

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

class MySubClass : MySuperClass
{
   ...
}

Then instantiate an instance of MySubClass...

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

MySubClass MySubClassInstance = new MySubClass();

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

然后测试它是否具有该属性...

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

MySubClassInstance <--- 现在具有 MyUberAttribute,其中“Bob”作为 SpecialName 值。

回答by ShuggyCoUk

Yes that is precisely what it means. Attribute

是的,这正是它的意思。属性

[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(true).First());

回答by Kolob Canyon

Attribute inheritance is enabled by default.

默认情况下启用属性继承。

You can change this behavior by:

您可以通过以下方式更改此行为:

[AttributeUsage (Inherited = False)]