1 2 (* I'm not sure if this code will work in Delphi 3 but it works from 4.x on up. *) 3 4 type 5 TForm1 = class(TForm) 6 Button1: TButton; 7 procedure Button1Click(Sender: TObject); 8 procedure SetCaption; 9 private 10 { Private declarations } 11 public 12 { Public declarations } 13 procedure DynamicExec(obj:TObject; MethodName: string) ; 14 15 end; 16 type 17 TExec = procedure of object; 18 var 19 Form1: TForm1; 20 21 implementation 22 23 {$R *.dfm} 24 25 { TForm1 } 26 27 procedure TForm1.DynamicExec(obj: TObject; MethodName: string); 28 var 29 DynMeth: TMethod; 30 Exec: TExec; 31 begin 32 DynMeth.Data := Pointer(obj) ; //assign pointer to object 33 DynMeth.Code := obj.MethodAddress(MethodName) ; //return address of objects 34 method. 35 if not Assigned(DynMeth.Code) then Exit; 36 Exec := TExec(DynMeth) ; //assign methon to Method to Method variable 37 Exec; //executes the method. 38 end; 39 40 procedure TForm1.Button1Click(Sender: TObject); 41 begin 42 DynamicExec(Form1,'SetCaption');//call to dynamically execute method by name 43 end; 44 45 procedure TForm1.SetCaption; 46 begin 47 caption:='Executed'; 48 end;