正如你所说,TMethod.Code是指向过程或函数的指针.
data是什么,我也不知道.看了Delphi下的一个源程序,学着做了一个例子,
但是,Data到底有什么意义也不很明确.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure RunProc(ProcName
Char);
published
procedure Hello;
procedure GoodBye;
end;
var
Form1: TForm1;
type
TMyProcedure=procedure of object;
implementation
{$R *.DFM}
procedure TForm1.Hello;
begin
ShowMessage('Hello');
end;
procedure TForm1.GoodBye;
begin
ShowMessage('GoodBye');
end;
procedure TForm1.RunProc(ProcName
Char);
var
theProc:TMyProcedure;
begin
try
//MethodAddress取得Published方法的地址
TMethod(theProc).Code:=Self.MethodAddress(ProcName);
if TMethod(theProc).Code = nil then Exit;
TMethod(theProc).Data := Self; //这句话加与不加没有区别?
theProc;
except
Exit;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RunProc('Hello');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
RunProc('GoodBye');
end;
end.