I want to use attributes for properties, but these attributes can change in the inherited class occasionaly. Here is a sample code (very simplified):
TBaseClass = class(TObject)
private
FFoo: string;
published
[BaseAttirib('hello')]
property Foo: string read FFoo;
end;
TChildClass = class(TBaseClass)
published
[BaseAttirib('good bye')]
property Foo;
end;
When Im going thru the properties, using RTTI, the Foo property appears twice in the GetProperties array:
var
xObj: TObject;
xType: TRttiType;
xProp: TRttiProperty;
begin
// FContext is a TRttiContext, already created, not included in this sample
xObj := TChildClass.Create;
try
xType := FContext.GetType(xObj.ClassType);
for xProp in xType.GetProperties do
begin
if not (xProp.Visibility in [mvPublished]) then
Continue;
Memo1.lines.add(xProp.Name + ' = ' + xProp.GetValue(xObj).AsString);
end;
finally
FreeAndNil(xObj);
end;
end;
Here is the output:
Foo = TChildClass
Foo = TChildClass
Changing property visibility to public in the base class would solve the problem, but that is not acceptable in my case, because I would have to increase this property visibility to published in all the child classes one by one.
My quesion is how can I prevent the Foo property duplicated appearance, or at least is there any way to decide between these duplications which one came from the base class and which one from the child?
UPDATE, little bit more explanation:
We have an algorytm which saves an object's all published properties in to an export file and the BaseAttrib attribute holds some neccessary information. Let's say I have an instance of TBaseClass and an instance of TChildClass and for the TBaseClass I want to see 'hello' in the output file and for the TChildClass I want to see 'good bye' in the output file.
Also worth to mention, if I wouldn't make instance of TBaseClass and lower the Foo visibility to public and then introduce a new instancable class where I publish the Foo property, I would lose the attribute for the Foo property (introduced in TBaseClass). So if I have 100 descendant classes but I just want to change the attribute value in only one class I would still have to copy the same attribute (with the original param) to the remaining 99 classes.
I would like to point that, this is a very simplified example, I have to introduce the attributes in an existing, complex class structure, and I want to do that with the least paintfull way without changing/rewrinting all the class inheritances.
If I could avoid/manage the property duplications that would be the best way, thats why Im looking for that kind of solution.