Execute a method via typeInfo

0

I'm studying RTTI in Lazarus and ran into a problem, trying to run procedure Clear; . I used the GetMethodProp function, as recommended in the Lazarus documentation, but the strange thing is that this method has as parameters an object and the name of a property that one wants to access, but I'm trying to access a method, not a property. I would like to know how I can get and execute the Clear method via the typeInfo. Below is my code and the error returned is Unknown property: "clear" in line methodInfo := GetMethodProp(dto, 'Clear'); .

unit Unit1;

{$mode objfpc}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, typinfo;

type
  TExec = procedure of Object;    

  { TDto }
  TDto = class
  strict private   
    fNome:    String;
    FApelido: String;
  public
    procedure Clear;
    constructor Create;
  published
    property Nome:      String read fNome write fNome;
    property Sobrenome: String read fNome write fNome;
  end;

  { TForm1 }
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    dto: TDto;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TDto }
Procedure Tdto.Clear;
Begin
  fNome    := '';
  FApelido := '';
End;

Constructor Tdto.Create;
Begin
  Clear;
End;

{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
  methodInfo: TMethod;
begin
  dto := TDto.Create;
  dto.Nome := 'Teste';

  methodInfo := GetMethodProp(dto, 'Clear');
  TExec(methodInfo);

  dto.Clear;
end;

end.
    
asked by anonymous 14.03.2018 / 13:10

0 answers