delphimethodsdelphi-2010rttirecords

Delphi - RTTI info about methods in records


how to extract RTTI info about methods in Delphi records? is it possible by using new Rtti unit?


Solution

  • There is no RTTI for methods on records, sorry.


    Update: As of Delphi XE2 this is now possible, see online help: https://docwiki.embarcadero.com/CodeExamples/en/TRttiRecordType_(Delphi)

    program Program1;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils,
      Rtti;
    
    type
      TKey = record
      private
        FData: array[0..15] of Byte;
      public
        function GetByte(Index: Integer): Byte;
        // ...
      end;
    
    function TKey.GetByte(Index: Integer): Byte;
    begin
      // ...
    end;
    
    var
      LContext: TRttiContext;
      LType: TRttiType;
      LRecord: TRttiRecordType;
      LMethod: TRttiMethod;
    
    begin
      LContext := TRttiContext.Create;
    
      LType := LContext.GetType(TypeInfo(TKey));
      if LType.IsRecord then
      begin
        LRecord := LType.AsRecord;
    
        { List all TKey methods }
        for LMethod in LRecord.GetMethods do
        begin
          Writeln(LMethod.ToString);
        end;
      end;
    
      LContext.Free;
    
    end.