我在程序中掉用自己写好的过程,一般都是 表名.过程名();
就可以了,现在我想把过程放在字符变量中,那怎样通过这个变量去执行相应的过程呢var
pro:string;
begin
pro:='mess';
self.pro//等同执行了 self.mess;
end;
就可以了,现在我想把过程放在字符变量中,那怎样通过这个变量去执行相应的过程呢var
pro:string;
begin
pro:='mess';
self.pro//等同执行了 self.mess;
end;
方法1:
1.把过程定义到published区,便于程序在运行期找到RTTI信息
2.使用MethodAddress方法来调用
方法2:
uses
objAuto;
ObjectInvoke
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure proc;
private
{ Private declarations } public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }
type
TExecute = procedure of object;
procedure TForm1.proc;
begin
showmessage('aaa');
end;procedure TForm1.Button1Click(Sender: TObject);
var
Routine: TMethod;
Execute: TExecute;
begin
Routine.Data:= Pointer(sender);
Routine.Code:= self.MethodAddress('proc');
if Routine.Code = nil then Exit;
Execute := TExecute(Routine);
Execute;
end;end.