C#属性继承不能像我期望的那样工作

Fio*_*ite 4 c# inheritance attributes properties

我在基类中有一个属性,其中包含一些属性:

[MyAttribute1]
[MyAttribute2]
public virtual int Count
{
  get
  {
    // some logic here
  }
  set
  {
    // some logic here
  }
}
Run Code Online (Sandbox Code Playgroud)

在一个派生类中,我已经这样做了,因为我想将MyAttribute3添加到属性中,我无法编辑基类:

[MyAttribute3]
public override int Count
{
  get
  {
     return base.Count;
  }
  set
  {
     base.Count = value;
  }
Run Code Online (Sandbox Code Playgroud)

}

但是,该属性现在表现得就像它上面没有MyAttribute1和MyAttribute2.我做错了什么,或属性不继承?

Lee*_*Lee 10

默认情况下不会继承属性.您可以使用以下AttributeUsage属性指定:

[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class MyAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)