Executando um metodo(Function) pelo nome
Olá, Boa tarde a todos!!!
Preciso da ajuda de vocês....
Alguém já executou um método(Function) pelo nome?
O exemplo abaixo consegui executar, porém chamando uma procedure, gostaria de fazer o mesmo, mas chamando uma function com parâmetros.
Segue o exemplo que fiz:
procedure ExecMethod(Func: string);var Routine: TMethod; Exec: TExec;begin Routine.Data := Application; Routine.Code := MethodAddress(Func); if Assigned(Routine.Code) then begin Exec := TExec(Routine); Exec; end;end;
ExecMethod('MostraConteudo');
Muito Obrigada e aguardo o Retorno de vocês.
--
Com Estima,
Lorena Coelho
Preciso da ajuda de vocês....
Alguém já executou um método(Function) pelo nome?
O exemplo abaixo consegui executar, porém chamando uma procedure, gostaria de fazer o mesmo, mas chamando uma function com parâmetros.
Segue o exemplo que fiz:
procedure ExecMethod(Func: string);var Routine: TMethod; Exec: TExec;begin Routine.Data := Application; Routine.Code := MethodAddress(Func); if Assigned(Routine.Code) then begin Exec := TExec(Routine); Exec; end;end;
ExecMethod('MostraConteudo');
Muito Obrigada e aguardo o Retorno de vocês.
--
Com Estima,
Lorena Coelho
Lorena
Curtidas 0
Melhor post
Marco Salles
31/08/2010
bem , no delphi 2010 é bem mais fácil devido ao novo RTTI
Mas versões anteriores com um pouco mais de sacrificio se obtem o mesmo resultado
Veja
published
function MostraConteudo(str: String):String;
...
function TForm6.MostraConteudo(str: String):String;
begin
result:=str; // so a titulo de exemplo
end;
Altere
type
TExec = function(str:String):string of object; // Altere o Tipo
Agora faça :
function ExecMethod(Func: string;str:String):String;
var
Routine: TMethod;
Exec: TExec;
begin
Routine.Code := form6.MethodAddress(Func);
if Assigned(Routine.Code) then
begin
Exec := TExec(Routine);
result:=Exec(str); // Aqui passa os Parametros
end;
end;
Para chamar faça
procedure TForm6.Button1Click(Sender: TObject);
var
str:String;
begin
Str:='Qualquer Coisa'
showmessage(ExecMethod('MostraConteudo',str));
end;
GOSTEI 1
Mais Respostas
Leonardo Xavier
31/08/2010
Lorena achei o seguinte: Ve se ajuda você.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, JvExButtons, JvButtons, DB, DBClient,
wwclient, JvDataSource, Grids, DBGrids, JvgDBGrid, JvExControls,
JvXPCore, JvXPButtons;
type
TForm1 = class(TForm)
Button1: TJvHTButton;
procedure Button1Click(Sender: TObject);
procedure CallMeByName(Sender: TObject);
private
procedure ExecMethod(OnObject: TObject; MethodName: string);
public
{ Public declarations }
end;
var
Form1: TForm1;
type
TExec = procedure of object;
implementation
uses
unit2;
{$R *.dfm}
{................................................................ ExecMethod }
procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string);
var
Routine: TMethod;
Exec : TExec;
begin
Routine.Data := Pointer(OnObject);
Routine.Code := OnObject.MethodAddress(MethodName);
if not Assigned(Routine.Code) then
Exit;
IF not Assigned(Routine.Code) then
Exit;
Exec:= TExec(Routine);
Exec;
end;
{.............................................................. CallMeByName }
procedure TForm1.CallMeByName(Sender: TObject);
begin
ShowMessage('Hello Delphi!');
end;
{.............................................................. Button1Click }
procedure TForm1.Button1Click(Sender: TObject);
begin
ExecMethod(Form1, 'CallMeByName');
end;
end.
//fonte:http://www.planetadelphi.com.br/dica/7469/-usando-variaveis-do-tipo-tprocedure-para-chamadas-pelo-nome-
GOSTEI 0
Lorena
31/08/2010
Lorena achei o seguinte: Ve se ajuda você.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, JvExButtons, JvButtons, DB, DBClient,
wwclient, JvDataSource, Grids, DBGrids, JvgDBGrid, JvExControls,
JvXPCore, JvXPButtons;
type
TForm1 = class(TForm)
Button1: TJvHTButton;
procedure Button1Click(Sender: TObject);
procedure CallMeByName(Sender: TObject);
private
procedure ExecMethod(OnObject: TObject; MethodName: string);
public
{ Public declarations }
end;
var
Form1: TForm1;
type
TExec = procedure of object;
implementation
uses
unit2;
{$R *.dfm}
{................................................................ ExecMethod }
procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string);
var
Routine: TMethod;
Exec : TExec;
begin
Routine.Data := Pointer(OnObject);
Routine.Code := OnObject.MethodAddress(MethodName);
if not Assigned(Routine.Code) then
Exit;
IF not Assigned(Routine.Code) then
Exit;
Exec:= TExec(Routine);
Exec;
end;
{.............................................................. CallMeByName }
procedure TForm1.CallMeByName(Sender: TObject);
begin
ShowMessage('Hello Delphi!');
end;
{.............................................................. Button1Click }
procedure TForm1.Button1Click(Sender: TObject);
begin
ExecMethod(Form1, 'CallMeByName');
end;
end.
//fonte:http://www.planetadelphi.com.br/dica/7469/-usando-variaveis-do-tipo-tprocedure-para-chamadas-pelo-nome-
Então Leonardo, tirei por base este exemplo que me passou, só que com ele só consigo executar procedures e não consigo executar funcoes com parametros, entendeu?
GOSTEI 0
Lorena
31/08/2010
Muito Obrigada Marcos, este é o que eu precisava.
Fique com Deus e muito obrigada a todos!!!
Ass: Lorena
Fique com Deus e muito obrigada a todos!!!
Ass: Lorena
GOSTEI 0
Carlos Mazzi
31/08/2010
OK, pode fechar o topico?
GOSTEI 0