访问Delphi BPL中的公共方法和属性

DRo*_*kie 5 delphi module bpl

我有一个应用程序加载一个简单形式的BPL.

此表单是主应用程序的可选选项.

BPL正确加载,表单显示正确,但我不知道如何访问bpl中表单的公共方法和属性.

有谁可以提供一个简单的例子?

我的代码:

// Load the BPL on aplication Load
LoadPackage( 'About.bpl' );

// CAll for TForm1 inside the About.BPL
var
  AClass: TClass;
  AForm: TForm;
begin

    AClass := GetClass('TForm1');
    if AClass <> nil then
  begin
        Application.CreateForm(TComponentClass(AClass), AForm);
        AForm.Show;
    end;

// The unit TForm1 inside the BPL package
unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

type
    TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
        PublicMthd;
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

Procedure TForm1.PublicMthd;
Begin
    ShowMessage('Inside call');
End;

initialization
    RegisterClass(TForm1);

finalization
    UnRegisterClass(TForm1);

end.
Run Code Online (Sandbox Code Playgroud)

如何在Tform1中访问"PublicMthd"?

Fra*_*ois 9

在动态加载的bpl中使用TOptionalForm的一个好处是(假设这来自"可选"位))是为了避免你的应用程序专门保存TOptionalForm类的定义(它在包中包含的单元中,只在那里).

这意味着除非您使用以下任
一项,否则您的应用程序无法了解它:- 共享基类
- 声明要访问的属性和方法的接口
- 一些用于访问已发布属性和方法的基本RTTI
- 一些扩展RTTI以访问公共属性和方法(如果你有D2010或更高版本)
- 来自bpl的一些外部例程接受基类参数(或TObject/pointer)在内部将其类型化为TOptionalForm.

这是非常模糊和一般的,需要更精确的代码来改进...