본문 바로가기

카테고리 없음

[일반/컴포넌트] 문자열 프로시저명으로 실제 프로시저 호출하기

// 주의) 아래 소스의 단점은 각 프로시저의 파라미터를 넘길 수 없습니다

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TMyProc = Procedure;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Procedure ExecProc(MyProcStr: String);
  Published
    Procedure MyProc1;
    Procedure MyProc2;
    Procedure MyProc3;
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.ExecProc(MyProcStr : String);
var
  MyProc: TMyProc;
begin
  // published method의 시작번지를 구한다
  MyProc := MethodAddress(MyProcStr);
  if Assigned(MyProc) then
    asm
      Call MyProc
    end;
end;

procedure TForm1.MyProc1;
begin
  ShowMessage('Proc 1');
end;

procedure TForm1.MyProc2;
begin
  ShowMessage('Proc 2');

end;

procedure TForm1.MyProc3;
begin
  ShowMessage('Proc 3');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecProc('MyProc1');
  ExecProc('MyProc2');
  ExecProc('MyProc3');
end;

end.