TActivator.Createinstance

9 views
Skip to first unread message

Gerard Hook

unread,
Jun 24, 2024, 6:47:43 PM (10 days ago) Jun 24
to Spring4D
Hi Stephan. I've just started using Spring4D and it seems amazing.
My current requirement is to create class instances from text only. I've tested this with TButton and all is as expected.

However, below i am trying to do the same with my composite frame as shown.
For some reason, TActivator.CreateInstance only works if the FClass assignment is performed first. I found this out accidentally while testing other scenarios. 
  var
    FClass: TClass;
  var
    F3: TFrame;
  FClass := frameKnowledgebase.TfrmKnowledgebase;  <<<<<
  F3 := TActivator.CreateInstance('frameKnowledgebase.TfrmKnowledgebase', [Self]) as TFrame;

______________________________________________________________________________
Exception position below.
______________________________________________________________________________
class function TActivator.FindConstructor(const classType: TRttiInstanceType;
  const arguments: array of TValue): TRttiMethod;

  function Assignable(const params: TArray<TRttiParameter>;
    const args: array of TValue): Boolean;
  var
    i: Integer;
    v: TValue;
  begin
    Result := Length(params) = Length(args);
    if Result then
      for i := Low(args) to High(args) do
        if not args[i].TryCast(params[i].paramType.Handle, v) then
          Exit(False);
  end;

var
  method: TRttiMethod;
begin
  for method in classType.GetMethods do <<<Exception occurs here!!!>>>
  begin
    if not method.IsConstructor then
      Continue;
Reply all
Reply to author
Forward
0 new messages