Delphi 根據字串找到函數並執行的實例
關鍵字:MethodAddress:取得方法的位址,這個方法需要是published的。
實例代碼:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TShowInfo = procedure(info:string) of object; class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } published procedure ShowInfo(info:string); end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1 } prochowid1. : string); begin ShowMessage(info); end; procedure TForm1.Button1Click(Sender: TObject); var s:TShowInfo; begin @s := MethodAddress('ShowInfo'); //取得ShowInfo的位址if @s <> nil then //如果不為空begin s('中華人民共和國'); //執行end; end; end.
如有疑問請留言或到本站社區交流討論,感謝閱讀,希望能幫助大家,謝謝大家對本站的支持!