• 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;


  • 相关阅读:
    The first appliaction for "Hello World!"
    zone
    learn to study
    深入理解 Angular 2 变化监测和 ngZone
    看看吧
    生命周期钩子
    一个简单的todo
    依赖注入
    @Output()
    @Input
  • 原文地址:https://www.cnblogs.com/xtfnpgy/p/9285372.html
Copyright © 2020-2023  润新知