Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi use reflection in a class procedure for the getting dynamic class type

I want use reflection on the current class inside a class procedure/function (static method). How can I do without using the "Self" keyword? And without harcode the class name: this procedure should be override in the descendants.

class procedure AAA.SetTableAndSequence;
var
c : TRttiContext;
t : TRttiType;
begin
  c := TRttiContext.Create;
  try
    t := c.GetType(Self.ClassType);
    ...
  finally
   c.Free;
  end;
end;
like image 887
Alberto Avatar asked Oct 15 '25 04:10

Alberto


1 Answers

You can use ClassInfo and GetType:

class procedure AAA.SetTableAndSequence;
var
  c: TRttiContext;
  t: TRttiType;
begin
  t := c.GetType(ClassInfo);
  ...
end;
like image 179
Stefan Glienke Avatar answered Oct 18 '25 00:10

Stefan Glienke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!