Author: Jonas Bilinkevicius
Is there a way to get a list of all published methods for a given class?
Preferably, I'd like to fill a TList with references to them, but even the text
representation would work.
Answer:
Enumerate the published methods of a class and all its ancestor classes:
1 procedure EnumMethods(aClass: TClass; lines: TStrings);
2
3 type
4 TMethodtableEntry = packed record
5 len: Word;
6 adr: Pointer;
7 name: ShortString;
8 end;
9 {Note: name occupies only the size required, so it is not a true shortstring!
10 The actual entry size is variable, so the method table is not an array
11 of TMethodTableEntry!}
12
13 var
14 pp: ^Pointer;
15 pMethodTable: Pointer;
16 pMethodEntry: ^TMethodTableEntry;
17 i, numEntries: Word;
18 begin
19 if aClass = nil then
20 Exit;
21 pp := Pointer(Integer(aClass) + vmtMethodtable);
22 pMethodTable := pp^;
23 lines.Add(format('Class %s: method table at %p', [aClass.Classname,
24 pMethodTable]));
25 if pMethodtable <> nil then
26 begin
27 {first word of the method table contains the number of entries}
28 numEntries := PWord(pMethodTable)^;
29 lines.Add(format(' %d published methods', [numEntries]));
30 {make pointer to first method entry, it starts at the second word of the table}
31 pMethodEntry := Pointer(Integer(pMethodTable) + 2);
32 for i := 1 to numEntries do
33 begin
34 with pMethodEntry^ do
35 lines.Add(format(' %d: len: %d, adr: %p, name: %s', [i, len, adr, name]));
36 {make pointer to next method entry}
37 pMethodEntry := Pointer(Integer(pMethodEntry) + pMethodEntry^.len);
38 end;
39 end;
40 EnumMethods(aClass.ClassParent, lines);
41 end;
42
43 procedure TForm2.Button1Click(Sender: TObject);
44 begin
45 memo1.clear;
46 EnumMethods(Classtype, memo1.lines);
47 end;
|