<div dir='auto'>Thank you!</div><div class="gmail_extra"><br><div class="gmail_quote">On Dec 27, 2023 7:46 AM, Sven Barth via fpc-pascal <fpc-pascal@lists.freepascal.org> wrote:<br type="attribution" /><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
    <div>Am 26.12.2023 um 21:29 schrieb Amir---
      via fpc-pascal:<br />
    </div>
    <blockquote>
      
      <br />
      <div>On 12/26/23 01:14, Sven Barth via
        fpc-pascal wrote:<br />
      </div>
      <blockquote>
        
        <div dir="auto">
          <div>
            <div class="elided-text">
              <div dir="ltr">Amir--- via fpc-pascal
                <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>>
                schrieb am Di., 26. Dez. 2023, 07:03:<br />
              </div>
              <blockquote style="margin:0 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 />
  </div>
</blockquote></div><br></div>