I'm trying to find all Properties with a TStrings descendant type on an Object.
Here is what I've tried sofar:
Place a Memo and a Chart on a form and then this code.
procedure TForm1.FormCreate(Sender: TObject);
var
aObject: TObject;
begin
for aObject in GetItemsObjects(Chart1) do
Memo1.Lines.Add(aObject.ClassName);
end;
class function TForm1.GetItemsObjects(aObject: TObject): TArray<TObject>;
var
RttiProperty: TRttiProperty;
RttiType: TRttiType;
ResultList: TList<TObject>;
PropertyValue: TValue;
PropertyObject: TObject;
s: String;
begin
ResultList := TList<TObject>.Create;
try
RttiType := RttiContext.GetType(aObject.ClassType);
for RttiProperty in RttiType.GetProperties do
begin
PropertyValue := RttiProperty.GetValue(aObject);
if (not PropertyValue.IsObject) or (PropertyValue.IsEmpty) then
continue;
try
PropertyObject := PropertyValue.AsObject;
s := PropertyObject.ClassName;
if (PropertyObject is TStrings) then
ResultList.Add(PropertyObject)
else
ResultList.AddRange(GetItemsObjects(PropertyObject));
except
end;
end;
finally
Result := ResultList.ToArray;
ResultList.Free;
end;
end;
The problem is that I get an stack overflow. Even though I've tried to stop the recursion with this code:
if (PropertyObject is TStrings) then
ResultList.Add(PropertyObject)
else
ResultList.AddRange(GetItemsObjects(PropertyObject));