Delphi: Call a function whose name is stored in a string

后端 未结 10 1352
旧时难觅i
旧时难觅i 2020-12-08 05:30

Is it possible to call a function whose name is stored in a string in Delphi?

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 05:36

    With Delphi 2010 you can uses JSON and SuperObject to invoke method with parametters.

    http://code.google.com/p/superobject/source/browse/#svn/trunk

    If you need, there is also an xml parser to transform xml to json.

      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure TestMethod(const value: string);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    uses superobject;
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SOInvoke(Self, 'TestMethod', SO('{value: "hello"}'));
    end;
    
    procedure TForm1.TestMethod(const value: string);
    begin
      Caption := value;
    end;
    

提交回复
热议问题