Ver Mensaje Individual
  #3  
Antiguo 03-01-2016
Avatar de AgustinOrtu
[AgustinOrtu] AgustinOrtu is offline
Miembro Premium
NULL
 
Registrado: ago 2013
Ubicación: Argentina
Posts: 1.858
Reputación: 17
AgustinOrtu Es un diamante en brutoAgustinOrtu Es un diamante en brutoAgustinOrtu Es un diamante en brutoAgustinOrtu Es un diamante en bruto
Es posible usando Rtti, pero requiere Delphi 2010 o superior:

Código Delphi [-]
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  public
    procedure YellHello;
  end;

  TAlgunaClase = class
  public
    procedure Yell(const AText: string);
  end;

var
  Form1: TForm1;

implementation

uses
  Rtti;

procedure TForm1.Button1Click(Sender: TObject);
var
  c: TRttiContext;
  t: TRttiType;
  m: TRttiMethod;
  NombreMetodo: string;
begin
  t := c.GetType(ClassType);
  if t = NIL then
    Exit;

  NombreMetodo := 'YellHello';
  m := t.GetMethod(NombreMetodo);
  if m = NIL then
    Exit;

  m.Invoke(Self, []);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  c: TRttiContext;
  t: TRttiType;
  m: TRttiMethod;
  NombreMetodo: string;
  AlgunObjeto: TAlgunaClase;
begin
  t := c.GetType(TAlgunaClase);
  if t = NIL then
    Exit;

  NombreMetodo := 'Yell';

  m := t.GetMethod(NombreMetodo);
  if m = NIL then
    Exit;

  AlgunObjeto := TAlgunaClase.Create;
  try
    m.Invoke(AlgunObjeto, ['Hi!']);
  finally
    AlgunObjeto.Free;
  end;
end;

procedure TForm1.YellHello;
begin
  ShowMessage('TForm1: Hello world!');
end;

{ TAlgunaClase }

procedure TAlgunaClase.Yell(const AText: string);
begin
  ShowMessage('TAlgunaClase: ' + AText);
end;
Responder Con Cita