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

方法属性是否在C#中继承?,c#,inheritance,attributes,methods,C#,Inheritance,Attributes,Methods,基类中应用于抽象方法的属性是否应用于子类中的重写版本 我希望这个问题在没有示例的情况下足够清楚。它取决于属性本身的声明方式-请参见属性。它取决于属性 应用于属性类定义的属性带有属性[AttributeUsageAttribute.Inherited],该属性确定属性是否在派生类中继承 看看这个样品 [global::System.AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = fals

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


我希望这个问题在没有示例的情况下足够清楚。

它取决于属性本身的声明方式-请参见属性。

它取决于属性

应用于属性类定义的属性带有属性[AttributeUsageAttribute.Inherited],该属性确定属性是否在派生类中继承

看看这个样品

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

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

}

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

[AttributeUsage( Inherited = false)]
public class CustomAttribute : Attribute
{
}
建议阅读: