4

I have a property in a base class with some attributes on it:

[MyAttribute1]
[MyAttribute2]
public virtual int Count
{
  get
  {
    // some logic here
  }
  set
  {
    // some logic here
  }
}

In a deriving class I have done this, because I want to add MyAttribute3 to the property and I cannot edit the base class:

[MyAttribute3]
public override int Count
{
  get
  {
     return base.Count;
  }
  set
  {
     base.Count = value;
  }

}

However, the property is now behaving like it doesn't have MyAttribute1 and MyAttribute2 on it. Am I doing something wrong, or do attributes not inherit?

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
  • 2
    the code using the attributes is that your own? then please post that too. In short when getting the attributes you can choose whether or not to include attributes from higher op the inheritance tree – Rune FS Jun 01 '12 at 09:27
  • -doh- I was hoping that wasn't the case! No it's not my own code. I may be able to convince them to change it though I guess... – Fiona - myaccessible.website Jun 01 '12 at 09:30
  • AFAIK there is no attribute inheritance! You'll have to add MyAttribute1 and MyAttribute2 to the override property. – CodingBarfield Jun 01 '12 at 09:33
  • Possible duplicate of http://stackoverflow.com/questions/1240960/attributes-and-inheritance – Bridge Jun 01 '12 at 09:35
  • When you say it's "behaving like it doesn't have MyAttribute1 and MyAttribute2 on it", what's the context? How is the property being accessed, what's doing the accessing and what should the attributes be doing which they're not? – Steve Wilkes Jun 01 '12 at 09:41
  • OK a little more context... I'm using one of those crazy packages that generates a user interface for you based on the structure of the database. It generates object classes for me, which I can derive from to tweak as necessary. The attributes in question define how the property will appear in the UI, and I wanted to add an extra one. – Fiona - myaccessible.website Jun 01 '12 at 09:50
  • If there is something that can be changed in the base classes or attributes themselves, I can tell the guy that owns it and ask him to make the change though... – Fiona - myaccessible.website Jun 01 '12 at 09:52

2 Answers2

10

Attributes are not inherited by default. You can specify this using the AttributeUsage attribute:

[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class MyAttribute : Attribute
{
}
Lee
  • 142,018
  • 20
  • 234
  • 287
2

It appears to work fine for me, if you're just using the method .GetType().GetCustomAttributes(true) this doesn't acually return any attributes, even if you set Inherited=true.

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
    public MyAttribute()
    {
    }
}

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
sealed class MyAttribute1 : Attribute
{
    public MyAttribute1()
    {
    }
}

class Class1
{
    [MyAttribute()]
    public virtual string test { get; set; }
}

class Class2 : Class1
{
    [MyAttribute1()]
    public override string test
    {
        get { return base.test; }
        set { base.test = value; }
    }
}

Then get the custom attributes from class 2.

Class2 a = new Class2();

MemberInfo memberInfo = typeof(Class2).GetMember("test")[0];
object[] attributes = Attribute.GetCustomAttributes(memberInfo, true);

attributes shows 2 elements in the array.

Andy
  • 6,366
  • 1
  • 32
  • 37