Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to instantiate a class by its name in delphi?

I'd like to instantiate a class but I only have its name in a string. Is there a way?

like image 513
Ricardo Acras Avatar asked Mar 31 '09 13:03

Ricardo Acras


People also ask

How do I create an instance of a class in Delphi?

type TAncestor = class; TAncestorClass = class of TAncestor; TAncestor = class public constructor Create; virtual; class function CreateClass(const AId: string): TAncestor; class procedure RegisterClass(const AId: string; const AType: TAncestorClass); end; class function TAncestor.

What is a class in Delphi?

Classes in DelphiA Class consists of fields, methods and properties which defines character and behavior of an object. Instances of a Class are called as Objects and fields, methods are called as class members. -> A Field is essentially a variable that is part of an object.


2 Answers

This is from Delphi help (Delphi 2006, but also available from at least Delphi 7):

Syntax function GetClass(const AClassName: string): TPersistentClass;

Description Call GetClass to obtain a class from a class name. This class can be used as a parameter to routines that require a class. The Class must be registered before GetClass can find it. Form classes and component classes that are referenced in a form declaration (instance variables) are automatically registered when the form is loaded. Other classes can be registered by calling RegisterClass or RegisterClasses .

Here some sample code. Works as such only because TButton is a TControl and therefore the typecast is valid.

procedure TForm1.FormCreate(Sender: TObject);
begin
  RegisterClasses([TButton, TForm]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  CRef : TPersistentClass;
  AControl : TControl;
begin
  CRef := GetClass('TButton');
  if CRef<>nil then
  begin
     AControl := TControl(TControlClass(CRef).Create(Self));
     with AControl do
     begin
        Parent := Self;
        Width := 50;
        Height := 30;
     end;
  end;
end;
like image 70
Ralph M. Rickenbach Avatar answered Oct 23 '22 04:10

Ralph M. Rickenbach


When I needed to do that, I Built my own Object Factory that uses a specially subclassed TStringList, I'm currently using Delphi 7 so the string list class supports only attach a Object to a String, then I got to subclass TStringList to make it possible handle Class Types too, so now I can instantiate a object just passing it's class name to the factory. Works that way:

1st - Load a Singleton Object Factory;
2st - Register any object to the factory, could be in the initialization section of the unit;

The main Factory's methods could be: isClassRegistered, registerClass, instantiateClass(ClassName: STring): TObject;

This way I can instantiate any object, or use a previous instantiated object, or even, a subset of they.

I rather use a Enumerated type instead of a string to identify a Class.

Remarks: It's a very, very terse example, a completely functional code is more complex, but, belive me, not too much.

like image 29
Gedean Dias Avatar answered Oct 23 '22 04:10

Gedean Dias