SHARE
TWEET

Criar Formulário por String Via Rtti Delphi

jpfassis Jan 18th, 2020 (edited) 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit uReturn_TObject_Str;
  2.  
  3. interface
  4.  
  5. uses
  6. Forms, Controls, SysUtils,
  7. Rtti, Generics.collections;
  8.  
  9. Type
  10. EInstancia_Not_Null = class (Exception);
  11.  
  12. Type
  13. TReturn_TObject_Str = class
  14. class procedure InstanciarPorString(Str_Class:TValue; Owner:TWinControl);
  15. end;
  16.  
  17. implementation
  18.  
  19. uses
  20. typInfo;
  21.  
  22. class procedure TReturn_TObject_Str.InstanciarPorString(Str_Class:TValue; Owner:TWinControl);
  23. var
  24.   Form : TForm;
  25.   C : TRttiContext;
  26.   Instancia : TRttiInstanceType;
  27.   P : TRttiType;
  28.   Erro : String;
  29. begin
  30.   try
  31.     case Str_Class.Kind of tkString,tkLString, tkWString, tkUString:
  32.     begin
  33.       Erro:=Str_Class.AsString+'Classe Não encontrada'+sLineBreak+
  34.       '<Lembrete : verifique ortografia&nbsp; / Case Sensitive>'+sLineBreak;
  35.       instancia:= (C.FindType(Str_Class.AsString) as TRttiInstanceType);
  36.  
  37.        Form := Tform(instancia.MetaclassType.Create).Create(Owner);
  38.        Form.ShowModal;
  39.        Form.Free;
  40.  
  41.     end;
  42.     tkClassRef:
  43.     begin
  44.       Erro:='O parâmetro passado deve ser do tipo Tclass'+sLineBreak;
  45.       p:=C.GetType(Str_Class.AsClass);
  46.       instancia:= (C.FindType(p.QualifiedName) as TRttiInstanceType);
  47.  
  48.        Form := Tform(instancia.MetaclassType.Create).Create(Owner);
  49.        Form.ShowModal;
  50.        Form.Free;
  51.  
  52.     end;
  53.     else
  54.     begin
  55.       Erro:='O parâmetro passado não é válidado para a função'+sLineBreak;
  56.       abort;
  57.     end;
  58.     end;
  59.  
  60.   except
  61.   on e:Exception do
  62.   begin
  63.     raise EInstancia_Not_Null .Create(Erro+
  64.     'Mensagem Original'+sLineBreak+e.Message);
  65.   end;
  66.  
  67.   end;
  68. end;
  69.  
  70.  
  71. end.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top