• Delphi根据方法名调用方法




    type
      TForm1 = class(TForm)

      public



      published
        procedure DoJsCall(str:string);                                                    //必须声明方法为published
        function  DoJsCall2(str:string):Tobject;

      end;

      TProcedure = function(Param: string):Tobject of object;          //定义方法原型,方便调用


    var

      Form1: TForm1;

    procedure TForm1.DoJsCall(str: string);
    begin
      mmo1.Lines.Add(str);
    end;


    function TForm1.DoJsCall2(str: string): Tobject;
    begin
      mmo1.Lines.Add(str);
      Result := TObject(mmo1.Lines.Text);
    end;


    //根据方法名调用方法

    function ExecuteMethod(Obj: TObject; Name, Param: string):Tobject;
    var
      PMethod: TMethod;
      AProcedure: TProcedure;
    begin
      PMethod.Data := Pointer(Obj);
      PMethod.Code := Obj.MethodAddress(Name);
      if Assigned(PMethod.Code) then
      begin
        AProcedure := TProcedure(PMethod);
        Result := AProcedure(Param);
      end;
    end;


    调用:

    procedure TForm1.btnExecuteMethodClick(Sender: TObject);
    var
      s : string;
    begin
      mmo1.Text := '';
      ExecuteMethod(self ,'DoJsCall2','aaa');
      ShowMessage('Step1:'+mmo1.Text);
      s := PChar(ExecuteMethod(self ,'DoJsCall2','bbb'));
      ShowMessage('Step2:'+s);
    end;


  • 相关阅读:
    02.替换空格 (Java)
    01.二维数组中的查找 (Java)
    css
    CSS Selectors
    Golang Singleton
    TL;DR
    go get
    golang string、int、int64 float 互相转换
    Thrift支持的基本数据类型
    双亲委派模型
  • 原文地址:https://www.cnblogs.com/xtfnpgy/p/9285372.html
Copyright © 2020-2023  润新知