Delphi methods by name

by Jan Verhoeven, 15 November 2002

In Delphi you can call published methods by name.

function TPascalServerF.ExecProc(MyProcStr : String):boolean;
Var
  MyProc : procedure of object;
Begin
  TMethod(myProc).data:=self;
  TMethod(MyProc).code :=MethodAddress(MyProcStr);
  result:=true;
  if not Assigned(MyProc) Then
    result:=false
  else
    MyProc;
end;

The example code above is from my PascalServer program where I use a Pascal Script Interpreter that generates an event each time an unknown identifier is encountered, allowing you to respond with you own code.

Because I wanted to avoid a lengthy sequence of if.. else statements, I needed a way to call a method from the identifier name. The function listed above does exactly that.

Published methods

The method presented only works with published procedures and functions.

Access Errors

Originally I did not use a procedure of object but just procedure and did not bother about allocating the implicit self. This seemed to work, untill I started calling other methods from within the published method. Delphi was expecting a self pointer which was not there and generated an access error.