type
TForm1 = class(TForm)
public
published
procedure DoJsCall(str:string); //必须声明方法为published
function DoJsCall2(str:string):Tobject;
end;
TProcedure = function(Param: string):Tobject of object; //定义方法原型,方便调用
Form1: TForm1;
procedure TForm1.DoJsCall(str: string);
begin
mmo1.Lines.Add(str);
end;
function TForm1.DoJsCall2(str: string): Tobject;
begin
mmo1.Lines.Add(str);
Result := TObject(mmo1.Lines.Text);
end;
//根据方法名调用方法
function ExecuteMethod(Obj: TObject; Name, Param: string):Tobject;
var
PMethod: TMethod;
AProcedure: TProcedure;
begin
PMethod.Data := Pointer(Obj);
PMethod.Code := Obj.MethodAddress(Name);
if Assigned(PMethod.Code) then
begin
AProcedure := TProcedure(PMethod);
Result := AProcedure(Param);
end;
end;
调用:
procedure TForm1.btnExecuteMethodClick(Sender: TObject);
var
s : string;
begin
mmo1.Text := '';
ExecuteMethod(self ,'DoJsCall2','aaa');
ShowMessage('Step1:'+mmo1.Text);
s := PChar(ExecuteMethod(self ,'DoJsCall2','bbb'));
ShowMessage('Step2:'+s);
end;