我目前对使用字符串表单名称创建表单感到困惑(如在 Delphi 中是否可以通过其名称实例化一个类?)但我的表单有它自己的构造函数创建。
//-BASE CLASS-//
TBaseForm = class(TForm)
constructor Create(param1, param2: string); overload;
protected
var1, var2: string;
end;
constructor TBaseForm.Create(param1, param2: string);
begin
inherited Create(Application);
var1 := param1;
var2 := param2;
end;
//-REAL CLASS-//
TMyForm = class(TBaseForm)
end;
//-CALLER-//
TCaller = class(TForm)
procedure Btn1Click(Sender: TObject);
procedure Btn2Click(Sender: TObject);
end;
uses UnitBaseForm, UnitMyForm;
procedure TCaller.Btn1Click(Sender: TObject);
begin
TMyForm.Create('x', 'y');
end;
procedure TCaller.Btn1Click(Sender: TObject);
var PC: TPersistentClass;
Form: TForm;
FormBase: TBaseForm;
begin
PC := GetClass('TMyForm');
// This is OK, but I can't pass param1 & 2
Form := TFormClass(PC).Create(Application);
// This will not error while compiled, but it will generate access violation because I need to create MyForm not BaseForm.
FormBase := TBaseForm(PC).Create('a', 'z');
end;
根据我提供的代码,如何仅通过字符串表单名称来创建动态自定义构造函数表单?还是真的不可能?(我开始认为这是不可能的)