Jump to content
msd

Items of custom managed records

Recommended Posts

Hello,

 

I have custom managed type for example:

 

  TvatIndividual = record
    DocumentNumber: string;
    TurnoverDate: TDate;
    PaymentDate: TDate;
    DocumentType: string;
    Year: integer;
    TurnoverDescription: string;
    TurnoverAmount: Currency;

  end;

 

I need to iterate over this record type and access its fields. For example, if I have 7 items, how can I retrieve the field names and their types using a loop in Delphi?

 

Thanks in advance!

Share this post


Link to post
program Project1;

{$APPTYPE CONSOLE}

uses
  System.Rtti;

type
  TvatIndividual = record
    DocumentNumber: string;
    TurnoverDate: TDate;
    PaymentDate: TDate;
    DocumentType: string;
    Year: integer;
    TurnoverDescription: string;
    TurnoverAmount: Currency;
  end;

begin
  for var f in TRttiContext.Create.GetType(TypeInfo(TvatIndividual)).GetFields do
    WriteLn(f.ToString);

  ReadLn;
end.

 

  • Like 1
  • Thanks 2

Share this post


Link to post

Just in case anybody else is wondering: This works since Delphi 2010 which was the version that first included the unit rtti. Older Delphi versions did not support RTTI for records.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×