2005. február 7., hétfő

How to get a list of all published methods for a given class


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.


Enumerate the published methods of a class and all its ancestor classes:

procedure EnumMethods(aClass: TClass; lines: TStrings);

  TMethodtableEntry = packed record
    len: Word;
    adr: Pointer;
    name: ShortString;
  {Note: name occupies only the size required, so it is not a true shortstring!
        The actual entry size is variable, so the method table is not an array
        of         TMethodTableEntry!}

  pp: ^Pointer;
  pMethodTable: Pointer;
  pMethodEntry: ^TMethodTableEntry;
  i, numEntries: Word;
  if aClass = nil then
  pp := Pointer(Integer(aClass) + vmtMethodtable);
  pMethodTable := pp^;
  lines.Add(format('Class %s: method table at %p', [aClass.Classname, pMethodTable]));
  if pMethodtable <> nil then
    {first word of the method table contains the number of entries}
    numEntries := PWord(pMethodTable)^;
    lines.Add(format('  %d published methods', [numEntries]));
    {make pointer to first method entry, it starts at the second word of the table}
    pMethodEntry := Pointer(Integer(pMethodTable) + 2);
    for i := 1 to numEntries do
      with pMethodEntry^ do
        lines.Add(format('  %d: len: %d, adr: %p, name: %s', [i, len, adr, name]));
      {make pointer to next method entry}
      pMethodEntry := Pointer(Integer(pMethodEntry) + pMethodEntry^.len);
  EnumMethods(aClass.ClassParent, lines);

procedure TForm2.Button1Click(Sender: TObject);
  EnumMethods(Classtype, memo1.lines);

Nincsenek megjegyzések:

Megjegyzés küldése