views:

99

answers:

1

I played with attributes and assumed that they are inherited but it doesn't seem so:

type
  [MyAttribute]
  TClass1 = class
  end;

  TClass2 = class(TClass1)
  end;

TClass2 doesn't have the Attribute "MyAttribute" although it inherits from Class1. Is there any possibility to make an attribute inheritable? Or do I have to go up the class hierarchy and search for attributes?

A: 

I never used attributes in Delphi - so this answer is kind of speculation. But I know about annotations in Java which is basically the same thing.

But it makes sense if they are not inherited: a subclass might require other attributes, or contradict attributes from a super class. Also, if attributes are not inherited you have a chance to follow the hierarchy if "your" attribute usecase needs that. If they were inherited you would have trouble to detect whether an attribute is actually on a particular class as opposed to any of its superclasses.

If you need inheritance and don't want to look at super classes it might make more sense to use a class function, or class property, or even a tag interface (declares no methods) instead. Those are inherited.

deepc
I use form inheritence and have an attribute which controls unit testing of those forms. I wanted that all subclasses/forms are automatically tested...now I'm iterating the class hierarchy upwards to look if this attribute is present.
Stebi