[fpc-pascal] Dynamically Execute method

Mattias Gaertner nc-gaertnma at netcologne.de
Tue Jun 19 09:37:39 CEST 2012


On Tue, 19 Jun 2012 00:22:29 +0300
ik <idokan at gmail.com> wrote:

> Hi,
> 
> I'm playing a bit with the notion of execute a method dynamically on demand.
> I'ved created the following PoC:
> https://gist.github.com/2950789
> 
> It works, but for some strange reason, I find myself feeling that I'm
> missing something, or it should be done differently.
> Does this code written properly, or should I do it differently ?

function ExecMethod(Instance : TObject; Name : String) : Boolean;
var Exec : TMethod;
begin
  Exec.Code := TProcedure(Instance.MethodAddress(Name));
  Exec.Data := Instance;
  Result := Assigned(Exec.Code);
  if Result then TProcedureOfObject(Exec)();
end;


Mattias



More information about the fpc-pascal mailing list