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

无人久伴 提交于 2020-01-04 03:37:15

问题


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;

回答1:


You can use ClassInfo and GetType:

class procedure AAA.SetTableAndSequence;
var
  c: TRttiContext;
  t: TRttiType;
begin
  t := c.GetType(ClassInfo);
  ...
end;


来源:https://stackoverflow.com/questions/24559016/delphi-use-reflection-in-a-class-procedure-for-the-getting-dynamic-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!