How to call constructor of given instance?

Viewed 255

My first object is TGoodFairy (TObject) and it has own children FChildren (TList). There is Assign procedure, which makes copy of any TGoodFairy and its children.

The second object is TBadFairy which is descendant of TGoodFairy.

I am struggling how to use Assign method of the TBadFairy to create TBadFairy children. I would like to use ClassType in connection with Create, in order to create right children for TBadFairy (for now only TGoodFairy children are created)

  TGoodFairy = class (TObject)
  private
    FChildren:TList;
    FName:string;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Assign(Source:TGoodFairy);
  published
    property Name:string read FName write FName;
  end;

  TBadFairy = class (TGoodFairy)
    procedure Assign(Source:TBadFairy);
  end;

------------------------

constructor TGoodFairy.Create;
begin
inherited;
FChildren:=TList.Create;
end;

destructor TGoodFairy.Destroy;
var i:integer;
begin
for i:=0 to FChildren.Count-1 do TGoodFairy(FChildren[i]).Free;
FChildren.Clear;
FChildren.Free;
inherited;
end;

procedure TGoodFairy.Assign(Source:TGoodFairy);
var i:integer;
    f:TGoodFairy;
    C:TClass;
begin
    FName:=Source.Name;
    for i:=0 to Source.FChildren.Count-1 do
    begin
    //C := Source.ClassType;
    //f := C.Create; 
    f := TGoodFairy.Create;  //<-- this should be parametrized somehow
    f.Assign(Source.FChildren[i]);
    FChildren.Add(f);
    end;
end;


procedure TBadFairy.Assign(Source:TBadFairy);
begin
inherited Assign(Source);
end;
1 Answers
Related