从类引用(Delphi)创建的表单执行方法

Chr*_*ian 2 delphi packages runtime

我有一个表单(form2),我实现了以下PUBLIC方法:

function ShowInterface(i:integer):boolean;
Run Code Online (Sandbox Code Playgroud)

此表单位于动态加载的包中.现在我想实例化这个表单(form2)并执行上面的方法.

重要提示:我无法在form1中引用form2的单元.

我尝试了这段代码,但它从未找到"ShowInterface"指针(返回nil).

procedure TfrmForm1.Button1Click(Sender: TObject);
var
  PackageModule: HModule;
  AClass: TPersistentClass;
  ShowInterface: function (i:integer):boolean;
  frm: TCustomForm;
begin
  PackageModule := LoadPackage('form2.bpl');
  if PackageModule <> 0 then
  begin
    AClass := GetClass('TfrmForm2');
    if AClass <> nil then // <<-- FINE!! IT FINDS OUT 'TfrmForm2' in 'form2.bpl')
    begin
      frm := TComponentClass(AClass).Create(Self) as TCustomForm;
      ShowInterface := frm.MethodAddress('ShowInterface'); // <<-- HERE!! ALLWAYS RETURNS "NIL"
      if @ShowInterface <> nil then
        ShowInterface(1);
      // but if I call frm.Show, it works fine. frm is "loaded"!!!

      frm.Free;
    end;
    DoUnloadPackage(PackageModule);
  end;
end;
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Mas*_*ler 5

MethodAddress仅适用于已发布的方法.将它移动到已发布的部分,它应该工作.

或者,如果您有Delphi 2010,扩展RTTI提供了一种按名称查找公共方法的方法.(或其他可见性级别,如果您从默认值更改它.)