Delphi: RTTI and TObjectList<TObject>
Posted
by conciliator
on Stack Overflow
See other posts from Stack Overflow
or by conciliator
Published on 2010-04-01T08:26:02Z
Indexed on
2010/04/01
8:33 UTC
Read the original article
Hit count: 623
Based on one answer to an earlier post, I'm investigating the possibility of the following design
TChildClass = class(TObject)
private
FField1: string;
FField2: string;
end;
TMyClass = class(TObject)
private
FField1: TChildClass;
FField2: TObjectList<TChildClass>;
end;
Now, in the real world, TMyClass will have 10 different lists like this, so I would like to be able to address these lists using RTTI. However, I'm not interested in the other fields of this class, so I need to check if a certain field is some sort of TObjectList. This is what I've got so far:
procedure InitializeClass(RContext: TRttiContext; AObject: TObject);
var
ROwnerType: TRttiType;
RObjListType: TRttiType;
RField: TRttiField;
SchInf: TSchemaInfoDetail;
begin
ROwnerType := RContext.GetType(AObject.ClassInfo);
RObjListType := RContext.GetType(TObjectList<TObject>);
for RField in ROwnerType.GetFields do begin
// How do I check if the type of TMyClass.FField2 (which is TObjectList<TChildClass>) is some sort of TObjectList?
end;
Clearly, RField.FieldType <> RObjListType.FieldType
. However, they do have some relation, don't they? It seems horrible (and wrong!) to make a very elaborate check for common functionality in order to make it highly probable that RField.FieldType
is in fact a TObjectList
.
To be honest, I am quite uncomfortable with generics, so the question might be very naïve. However, I'm more than happy to learn. Is the above solution possible to implement? TIA!
© Stack Overflow or respective owner