Search code examples
delphirtti

delphi rtti access array in record


I want to enumerate field information (name, type, …) of a record. RTTI delivers field name but type is null(nil)! How can I get this information?

Record:

 foo = record
  bar : array[0..5] of char;
 end;

Enumeration:

  for var f : TRttiField  in TRTTIContext.Create.GetType(TypeInfo(foo)).GetFields do
  begin
    OutputDebugString(PWideChar(f.Name + ' :: ' + f.FieldType.ToString())); ///fieldtype is nil??!
  end;

Solution

  • The RTTI system only works with predefined types. Defining field types "on-the-fly" does not generate RTTI information. So, declare the array type like this instead:

    type
      TChar5Arr = array[0..5] of Char;
    
      foo = record
        bar : TChar5Arr;
      end;
    

    And you will get some more info:

    name: bar
    type: TChar5Arr
    value: (array)    //is not retrieved using GetValue