简体   繁体   English

自由帕斯卡上的虚拟方法表

[英]Virtual Method Table on Free Pascal

What I'm trying to do is to get the list of fields in a class without an instance... for example: 我想做的是获取没有实例的类中的字段列表...例如:

TAClass=class

a_: Integer;
b_: Integer;

constructor (a,b Integer);

end;

I'm not being able to get the fieldTable from the VMT: 我无法从VMT获取fieldTable:

ovmt: PVmt;
ftable: PVmtFieldTable;
finfo: PVmtFieldEntry;

ovmt:=PVmt(TAClass);
ftable := ovmt^.vfieldtable
finfo := ftable^.fields[0]

this way I'm not gettig the list of fields 这样我就不会得到字段列表

any help is welcome, thanks in advance 欢迎任何帮助,在此先感谢

Afaik the field tables in classic delphi and FPC only work for published fields. Afaik经典delphi和FPC中的字段表仅适用于已发布的字段。 Published fields must be class fields (value types like integer must go via properties). 发布的字段必须是类字段(值类型(如整数)必须通过属性)。 Newer Delphi's also allow RTTI for non published fields, but that works differently (different untis), and FPC doesn't support that yet. 较新的Delphi还允许RTTI用于未发布的字段,但是工作方式不同(不同的tis),FPC尚不支持。

I hacked together a small demonstration example since the help for typinfo seems to be light on examples. 我一起整理了一个小型的演示示例,因为typinfo的帮助似乎不多于示例。 Note the tpersistent derivation. 注意持久的推导。

{$mode delphi}

uses typinfo,classes;
type
  TAClass=class(Tpersistent)
                   a: tstringlist;
                   b: tlist;
                 end;


 var
      ovmt: PVmt;
      FieldTable: PVMTFieldTable;
      PVMTFieldEntry;
           i: longint;

    begin

         ovmt := PVmt(TAClass);
         while ovmt <> nil do
         begin
           FieldTable := PVMTFieldTable(ovmt^.vFieldTable);
           if FieldTable <> nil then
           begin
             FieldInfo := @FieldTable^.Fields[0];
             for i := 0 to FieldTable^.Count - 1 do
             begin
               writeln(fieldinfo^.name);
               FieldInfo := PvmtFieldEntry(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
             end;
           end;
           { Try again with the parent class type }
           ovmt:=ovmt^.vParent;
         end;

end. 结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM