program Project6;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Collections,
System.Rtti;
type
TMyObject =
class(TObject)
private
FDateTime:
string;
public
constructor Create;
end;
TMyContainer =
class
private
FList: TObjectList<TMyObject>;
public
constructor Create;
property List: TObjectList<TMyObject>
read FList;
end;
constructor TMyObject.Create;
begin
inherited;
FDateTime := FormatDateTime('
YYYY-MM-DD', System.SysUtils.Now);
end;
var
MyContainer: TMyContainer;
Data, ArrayObject: TObject;
FRTTICtx: TRttiContext;
rttiType: TRttiType;
rttiProperty: TRttiProperty;
Value: TValue;
SingleArrayValue: TValue;
I, Len: Integer;
{ TMyContainer }
constructor TMyContainer.Create;
begin
FList := TObjectList<TMyObject>.Create;
end;
begin
try
MyContainer := TMyContainer.Create;
MyContainer.List.Add(TMyObject.Create);
MyContainer.List.Add(TMyObject.Create);
MyContainer.List.Add(TMyObject.Create);
Data := MyContainer.List;
FRTTICtx := TRttiContext.Create;
rttiType := FRTTICtx.GetType(Data.ClassType);
for rttiProperty
in rttiType.GetProperties
do
begin
Writeln('
I found this Property: ', rttiProperty.
Name);
if rttiProperty.
Name = '
List'
then
begin
Writeln('
..That''
s the right one: ', rttiProperty.
Name);
case rttiProperty.PropertyType.TypeKind
of
TTypeKind.tkDynArray:
begin
Writeln('
....' + rttiProperty.
Name, '
is a TTypeKind.tkDynArray!');
Value := rttiProperty.GetValue(Data);
case Value.Kind
of
TTypeKind.tkDynArray:
begin
Len := Value.GetArrayLength;
for I := 0
to Len - 1
do
begin
SingleArrayValue := Value.GetArrayElement(I);
case SingleArrayValue.Kind
of
tkClass:
begin
ArrayObject := SingleArrayValue.AsObject;
// Len ist 3 => vier Elemente (?), so dass beim Zugriff auf das letzte
// Element nil rauskommt! Daher abfangen oder bessere Lösung suchen!
if Assigned(ArrayObject)
then
begin
Writeln('
.......Found single object: ', I, '
', ArrayObject.ClassName);
end;
end;
end;
end;
end
end;
end;
end;
end;
end;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
Readln;
end.