Dav*_*ois 6 delphi class delphi-7
我正在尝试做这样的事情:
function CreateIfForm ( const nClass : TClass ) : TForm;
begin
if not ( nClass is TFormClass ) then
raise Exception.Create( 'Not a form class' );
Result := ( nClass as TFormClass ).Create( Application );
end;
Run Code Online (Sandbox Code Playgroud)
这会产生错误"运算符不适用于此操作数类型".我正在使用Delphi 7.
Uli*_*rdt 18
首先,您应该检查是否可以将函数更改为仅接受表单类:
function CreateIfForm(const nClass: TFormClass): TForm;
Run Code Online (Sandbox Code Playgroud)
并且不需要进行类型检查和铸造.
如果这不合适,您可以使用InheritsFrom:
function CreateIfForm(const nClass: TClass): TForm;
begin
if not nClass.InheritsFrom(TForm) then
raise Exception.Create('Not a form class');
Result := TFormClass(nClass).Create(Application);
end;
Run Code Online (Sandbox Code Playgroud)