How to execute a method (procedure/function) by name
2004-10-25 10:06
731 查看
Here's a simple trick to execute a Delphi object's method by name:
~~~~~~~~~~~~~~~~~~~~~~~~~
type
牋 TForm1 = class(TForm)
牋牋 Button1: TButton;
牋牋 procedure Button1Click(Sender: TObject) ;
牋牋 procedure CallMeByName(Sender: TObject) ;
牋 private
牋牋 procedure ExecMethod(OnObject: TObject; MethodName: string) ;
牋 end;
var
牋 Form1: TForm1;
type
牋 TExec = procedure of object;
procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string) ;
var
牋 Routine: TMethod;
牋 Exec: TExec;
begin
牋 Routine.Data := Pointer(OnObject) ;
牋 Routine.Code := OnObject.MethodAddress(MethodName) ;
牋 if NOT Assigned(Routine.Code) then Exit;
牋 Exec := TExec(Routine) ;
牋 Exec;
end;
procedure TForm1.CallMeByName(Sender: TObject) ;
begin
牋 ShowMessage('Hello Delphi!') ;
end;
procedure TForm1.Button1Click(Sender: TObject) ;
begin
牋 ExecMethod(Form1, 'CallMeByName') ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
type
牋 TForm1 = class(TForm)
牋牋 Button1: TButton;
牋牋 procedure Button1Click(Sender: TObject) ;
牋牋 procedure CallMeByName(Sender: TObject) ;
牋 private
牋牋 procedure ExecMethod(OnObject: TObject; MethodName: string) ;
牋 end;
var
牋 Form1: TForm1;
type
牋 TExec = procedure of object;
procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string) ;
var
牋 Routine: TMethod;
牋 Exec: TExec;
begin
牋 Routine.Data := Pointer(OnObject) ;
牋 Routine.Code := OnObject.MethodAddress(MethodName) ;
牋 if NOT Assigned(Routine.Code) then Exit;
牋 Exec := TExec(Routine) ;
牋 Exec;
end;
procedure TForm1.CallMeByName(Sender: TObject) ;
begin
牋 ShowMessage('Hello Delphi!') ;
end;
procedure TForm1.Button1Click(Sender: TObject) ;
begin
牋 ExecMethod(Form1, 'CallMeByName') ;
end;
相关文章推荐
- How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- How to get only direct child elements by jQuery function
- How to rmname by using cleartool?
- How to get response content with specified post data by post method
- How to mock the function with output parameter by cppumock?
- How to avoid uppercase function name while using Function in latex?
- How to name a function
- How to create Function/Procedure in Oracle?
- How to debug stored procedure/function in TOAD
- How to get real file offset in NAND by file name?
- [ASP.NET2.0][数据库更新]How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- how to execute-shell-commands by ruby
- How to get report service instance name by wmi
- How to find report by report name in Windchill
- How to execute a Stored Procedure with Entity Framework Code First
- How to find a node by its text using the GetNodeByText method
- How to tell git format-patch to contain function name in your patch
- How to execute tons of tasks parallelly with TPL method?
- How to Use a Function or a Procedure as a Parameter in another Function
- How to call Oracle function or stored procedure using spring persistence framework?