首页 > 其他分享 >delphi通过方法名调用方法

delphi通过方法名调用方法

时间:2023-01-24 21:23:29浏览次数:37  
标签:begin 调用 end string delphi Vcl TForm1 方法 procedure

delphi通过方法名调用方法

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TFunc = procedure(Param: string) of object;          //定义方法原型

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  published
    procedure hello(p: string);   //必须声明为published方法
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//根据方法名调用方法

procedure ExecFunc(Obj: TObject; Name, Param: string);
var
  m: TMethod;
  f: TFunc;
begin
  m.Data := Pointer(Obj);
  m.Code := Obj.MethodAddress(Name);
  if Assigned(m.Code) then
  begin
    f := TFunc(m);
    f(Param);
  end;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecFunc(Self, 'hello', 'hello yn');
end;

procedure TForm1.hello(p: string);
begin
  ShowMessage(p);
end;

end.

 

标签:begin,调用,end,string,delphi,Vcl,TForm1,方法,procedure
From: https://www.cnblogs.com/hnxxcxg/p/17066399.html

相关文章