以下是測試代碼:
1program TestChar;
2
3{$APPTYPE CONSOLE}
4
5uses
6 SysUtils,
7 ObjAuto,
8 TypInfo,
9 AutoPtr in '..\..\DJSon\common\AutoPtr.pas',
10 Utils in '..\..\DJSon\common\Utils.pas';
11
12type
13{$METHODINFO ON}
14 TTestClass = class
15 public
16 function Test3: Integer;
17 procedure Test2(a: string);
18 function Test1(a: string; b: Single): Single;
19 end;
20{$METHODINFO OFF}
21
22var
23 t: TTestClass;
24
25{ TTestClass }
26
27function TTestClass.Test1(a: string; b: Single): Single;
28begin
29
30end;
31
32procedure TTestClass.Test2(a: string);
33begin
34
35end;
36
37function TTestClass.Test3: Integer;
38begin
39
40end;
41
42procedure TestIt;
43var
44 miArr: TMethodInfoArray;
45 mi: PMethodInfoHeader;
46 t: TTestClass;
47 retInfo: PReturnInfo;
48 piArr: TParamInfoArray;
49 pi: PParamInfo;
50 i: Integer;
51begin
52 t := TTestClass.Create;
53
54 miArr := GetMethods(TTestClass);
55 for mi in miArr do
56 begin
57 Writeln('Method: ' + mi.Name);
58
59 retInfo := GetReturnInfo(t, mi.Name);
60 if retInfo.ReturnType <> nil then
61 begin
62 Writeln('ReturnType: ' + retInfo.ReturnType^.Name);
63 end;
64
65 piArr := GetParams(t, mi.Name);
66 if piArr <> nil then
67 begin
68 for pi in piArr do
69 Writeln('Param Name: ' + pi.Name + ' Param Type: ' + pi.ParamType^.Name);
70 end;
71 end;
72
73 t.Free;
74end;
75
76begin
77 TestIt;
78 Readln;
79end.
代碼運行結果: