Delphi 根據字符串找到函數並執行的實例

這篇文章主要介紹了Delphi 根據字符串找到函數並執行的實例的相關資料,希望通過本能幫助到大家實現這樣的功能,需要的朋友可以參考下

Delphi 根據字符串找到函數並執行的實例

 關鍵字:MethodAddress:取得方法的地址,這個方法需要是published的。

實例代碼:

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls; 
 
type 
 
 TShowInfo = procedure(info:string) of object;  //聲明一個procedure類型,參數和ShowInfo一致 
 
 TForm1 = 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.ShowInfo(info: 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. 

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章