Access to TRTTIField.FieldType within classes when the array type is created ad-hoc

47 views Asked by At

I need to read all member variables of a class by using RTTI. I get for the field type nil if an array type is declared ad-hoc inside the class. If the same type is declared outside ( before) the class, reading the field type works as expected.

type 
  TMyClass = class
    MyArray: array[0..1] of double;
  end;

var
  MyClass: TMyClass;
  Ctx: TRTTIContext;
  T: TRTTIType;
  F: TRTTIField;
begin
  MyClass := TMyClass.Create;
  Ctx := TRTTIContext.Create;
  T := Ctx.GetType(MyClass.ClassInfo);
  for F in T.GetFields do
    case F.FieldType.TypeKind of // <-- AV because F.FieldType is nil in this situation
      tkFloat: ;
    end;
end;

F.DataType is also nil in this situation.

I don't see this issue if I declare the class like this:

type 
  TMyArr = array[0..1] of double;
  TMyClass = class
    MyArray: TMyArr;
  end;

If the array is declared as dynamic array (myArray: array of double) I don't see this problem.

Is there a workaround for this situation as I am developing a component where I do not write the declaration of the class myself?

I tested this code with Delphi 12 and 11.

0

There are 0 answers