如: 在Form1窗体中定义了一个方法MessageShow(str : string),
这个方法通过字符串保存在数据库某个字段中,如Field1这个字段中,现在我想通过从数据库中找到这个字符串,然后通过个字符串在Form1中调用巳定义好的方法,这个应该怎样写

解决方案 »

  1.   

    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure aaa();
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function ExecMethodByName(AObject: TObject; AName: string): variant;
      public    { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.aaa;
    begin
      showmessage( 'aaaa' );
    end;function TForm1.ExecMethodByName(AObject: TObject; AName: string): variant;
    var
      PAddr: Pointer;
    begin
      try
        PAddr := AObject.MethodAddress(AName);
        asm
          mov eax, AObject
          call PAddr
          mov Result, eax
        end;
      except
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      //取出aaa
      ExecMethodByName( self,'aaa' );
    end;
      

  2.   

    如果是类方法,这样就行:type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure MessageShow;//.....procedure TForm1.MessageShow;
    begin
      Showmessage('OK,但参数怎么办');
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      Proc : TProcedure;
      ProcName : string;
    begin
      ProcName := 'MessageShow';
      Proc := TForm1.MethodAddress(ProcName);
      if Assigned(Proc) then Proc;
    end;但,传参,怎么做?
      

  3.   

    hongqi162(失踪的月亮) : 执行这里的时候要报错  call PAddr;地址为空
      

  4.   

    type
      TMyMethod  = procedure(p: Integer) of object;  TForm1 = class(TForm)
        Button1: TButton;
        procedure bbb( i:integer );
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure InvokeMyMethod( Obj:TObject; const MethodName:String; param:Integer );
      public    { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      InvokeMyMethod( self,'bbb',1 );
    end;procedure TForm1.bbb(i: integer);
    begin
      showmessage( inttostr( i ) );
    end;procedure TForm1.InvokeMyMethod( Obj:TObject; const MethodName:String; param:Integer );
    var
      aMethod:TMethod;
    begin
      aMethod.Code:= Obj.MethodAddress( MethodName );
      aMethod.Data:= Obj;
      TMyMethod(aMethod)( param );
    end;end.
      

  5.   

    注意我的bbb方法定一的位置,必须是类对象中的published方法
      

  6.   

    type
      TMyMethod  = procedure(p: Integer) of object;嗯,感谢!
      

  7.   

    强烈要求 lihuasoft(学习低调做人) 开贴放分:)