メソッド名一覧の表示
Posted feedbacks - Delphi
いけるのはpublishedで宣言されたもののみです。 ちなみにお題のタイトルのようにメソッド名一覧を表示する場合は、 Pos関数内で使ってるPShortString(...)^をWritelnしてやればOKです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | program EnumMethodNames;
{$APPTYPE CONSOLE}
type
THoge = class(TObject)
published
procedure test_foo;
end;
THogeEx = class(THoge)
published
procedure test_bar;
procedure foobar;
end;
THogeMethod = procedure of object;
procedure THoge.test_foo;
begin
Writeln('foo');
end;
procedure THogeEx.test_bar;
begin
Writeln('bar');
end;
procedure THogeEx.foobar;
begin
Writeln('foobar');
end;
procedure EnumMethods(target: TObject);
var
ref: TClass;
table: PChar;
count: Word;
method: TMethod;
begin
ref := target.ClassType;
while ref <> nil do begin
table := PPointer(PChar(ref) + vmtMethodTable)^;
if Assigned(table) then begin
count := PWord(table)^;
Inc(table, SizeOf(Word));
while count > 0 do begin
if Pos('test_', PShortString(table+SizeOf(Word)+SizeOf(Pointer))^) = 1 then begin
method.Code := PPointer(table+SizeOf(Word))^;
method.Data := target;
THogeMethod(method);
end;
Inc(table, PWord(table)^);
Dec(count);
end;
end;
ref := ref.ClassParent;
end;
end;
var
Hoge: THoge;
begin
Hoge := THogeEx.Create;
try
EnumMethods(Hoge);
finally
Hoge.Free;
end;
end.
|

にしお
#3388()
Rating1/1=1.00
「ある与えられたオブジェクトtargetのメソッドのうち、 "test_"で始まるものをすべて呼びだす」というコードを書いてください。 引数に関しては都合のいいように仮定して構いません(全部0個、など)。
メソッドという概念がない言語の場合は、 「複数の関数への参照を持っているようなオブジェクト(たとえばパッケージとかモジュールとか)から"test_"で始まる関数をすべて呼び出す」と読み替えても構いません。
[ reply ]