Advertisement
jpfassis

Criar Formulário pela Classe Via Rtti Delphi

Jan 18th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.10 KB | None | 0 0
  1. unit uReturn_TObject;
  2.  
  3. interface
  4.  
  5. uses
  6. Forms, Controls, SysUtils,
  7. Rtti, Generics.collections;
  8.  
  9.  
  10. Type
  11. TReturn_TObject = class
  12. class function Instanciar<T:Class>:T;
  13. class procedure InstanciarForms<T:Class>(Owner:TWinControl);
  14. end;
  15.  
  16. implementation
  17.  
  18. uses
  19. typInfo;
  20.  
  21. { TReturn_TObject<T> }
  22.  
  23. class function TReturn_TObject.Instanciar<T>: T;
  24. var
  25. valor: TValue;
  26. ctx: TRttiContext;
  27. tipo: TRttiType;
  28. tipoInstancia: TRttiInstanceType;
  29. begin
  30. tipo := ctx.GetType(TypeInfo(T));
  31. tipoInstancia:= (Ctx.FindType(Tipo.QualifiedName) as TRttiInstanceType);
  32. Valor:=Tipoinstancia.MetaclassType.Create;
  33. Result := valor.AsType<T>;
  34. end;
  35.  
  36. class procedure TReturn_TObject.InstanciarForms<T>(Owner: TWinControl);
  37. var
  38.    Form:TForm;
  39.    valor: TValue;
  40.    ctx: TRttiContext;
  41.    tipo: TRttiType;
  42.    tipoInstancia: TRttiInstanceType;
  43. begin
  44.  tipo := ctx.GetType(TypeInfo(T));
  45.  tipoInstancia:=
  46.   (Ctx.FindType(Tipo.QualifiedName) as TRttiInstanceType);
  47.  Valor:=Tipoinstancia.MetaclassType.Create;
  48.  form := Tform(valor.AsType<T>).Create(Owner);
  49.  Form.ShowModal;
  50.  Form.Free;
  51. end;
  52.  
  53. { TReturn_TObjecStringt }
  54.  
  55. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement