First you need to check if you can change the function to accept only the form class:
function CreateIfForm(const nClass: TFormClass): TForm;
and get around the need for type checking and casting.
If this is not possible, you can use 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;
source share