<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">Am 26.12.2023 um 21:29 schrieb Amir---
      via fpc-pascal:<br>
    </div>
    <blockquote type="cite"
      cite="mid:a00888a2-2054-4917-8b7f-f793caa0768f@Aavani.net">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <br>
      <div class="moz-cite-prefix">On 12/26/23 01:14, Sven Barth via
        fpc-pascal wrote:<br>
      </div>
      <blockquote type="cite"
cite="mid:CAFMUeB8H1sE3O6ecuLX2s3Z0nHMhaRZYye-tkWUk1x0_NveNeg@mail.gmail.com">
        <meta http-equiv="content-type"
          content="text/html; charset=UTF-8">
        <div dir="auto">
          <div>
            <div class="gmail_quote">
              <div dir="ltr" class="gmail_attr">Amir--- via fpc-pascal
                <<a href="mailto:fpc-pascal@lists.freepascal.org"
                  moz-do-not-send="true" class="moz-txt-link-freetext">fpc-pascal@lists.freepascal.org</a>>
                schrieb am Di., 26. Dez. 2023, 07:03:<br>
              </div>
              <blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
                <br>
                   I want to retrieve the name of the fields in a
                record/class, at run <br>
                time. It looks like "TVmt.vFieldTable" is what I need.
                But I cannot find <br>
                any documentation about how to explore the content of
                this table. I <br>
                appreciate any pointer.<br>
              </blockquote>
            </div>
          </div>
          <div dir="auto"><br>
          </div>
          <div dir="auto">This only works for published fields and only
            fields of type class or interface can be published. <br>
          </div>
        </div>
      </blockquote>
      That would work for me. How can I enumerate over those fields?</blockquote>
    <br>
    You can use the PVmtFieldTable and PVmtFieldEntry types from the
    TypInfo unit:<br>
    <br>
    === code begin ===<br>
    <br>
    program tfield;<br>
    <br>
    {$mode objfpc}{$H+}<br>
    <br>
    uses<br>
      TypInfo;<br>
    <br>
    type<br>
      {$M+}<br>
      TSub = class<br>
    <br>
      end;<br>
    <br>
      TTest = class<br>
      published<br>
        fTest: TSub;<br>
      end;<br>
    <br>
    var<br>
      vft: PVmtFieldTable;<br>
      vfe: PVmtFieldEntry;<br>
      i: SizeInt;<br>
    begin<br>
      vft := PVmtFieldTable(PVMT(TTest)^.vFieldTable);<br>
      Writeln(vft^.Count, ' field(s) with ', vft^.ClassTab^.Count, '
    type(s)');<br>
      for i := 0 to vft^.Count - 1 do begin<br>
        vfe := vft^.Field[i];<br>
        Writeln(i, ' -> ', vfe^.Name, ' @ ', vfe^.FieldOffset, ' of
    type ', vft^.ClassTab^.ClassRef[vfe^.TypeIndex - 1]^.ClassName);<br>
      end;<br>
    end.<br>
    <br>
    === code end ===<br>
    <br>
    === output begin ===<br>
    <br>
    PS C:\fpc\git> .\testoutput\tfield.exe<br>
    1 field(s) with 1 type(s)<br>
    0 -> fTest @ 8 of type TSub<br>
    <br>
    === output end ===<br>
    <br>
    Side note: contrary to what I had originally written only classes,
    but not interfaces are allowed for published fields and they need to
    have $M enabled.<br>
    <br>
    Regards,<br>
    Sven<br>
  </body>
</html>