方法属性是否在C#中继承?

Ali*_*zmi 29 c# methods inheritance attributes

是否将属性应用于基类中的抽象方法应用于子类中的重写版本?

我希望这个问题很清楚,没有一个例子.

Pav*_*aev 33

这取决于属性本身的声明方式 - 请参阅AttributeUsageAttribute.Inherited属性.


thi*_*eek 17

这取决于属性.

应用于Attribute类defination的属性,带有一个属性[AttributeUsageAttribute.Inherited],用于确定是否在派生类中继承了一个属性.

看看这个样本

[global::System.AttributeUsage(AttributeTargets.Method, Inherited = true, 
     AllowMultiple = false)]
public sealed class MyAttribute : Attribute
{

    public MyAttribute (string FieldName)
    {
        //constructor.
    }

}
Run Code Online (Sandbox Code Playgroud)


CMS*_*CMS 9

您可以通过将AttributeUsage属性应用于自定义属性并设置Inherited属性(默认情况下为true)来指定该属性.此属性指示您的属性是否可以由从应用了自定义属性的类派生的类继承.

[AttributeUsage( Inherited = false)]
public class CustomAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)

推荐阅读: