delphi 调用字符串名称的函数

type
  Ptest = function(Str: string): string of object;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  methot: TMethod;
  celler: Ptest;
begin
  methot.Data := Self;
  methot.Code := Self.MethodAddress('foo');//绑定foo函数
  celler := ptest(methot);
  celler('foo');  //调用foo    =>弹出fOO1


  methot.Code := Self.MethodAddress('Goo');//绑定GOO
  celler := ptest(methot);
  celler('Goo');  //调用GOO    =>弹出GOO1
end;

procedure TForm1.foo();
begin
  ShowMessage('foo1');
end;

procedure TForm1.Goo();
begin
  ShowMessage('Goo1');
end;

感谢大佬指点

posted @ 2022-11-18 11:17  一曲轻扬  阅读(143)  评论(0编辑  收藏  举报