Delphi在类过程中使用反射来获取动态类类型 [英] Delphi use reflection in a class procedure for the getting dynamic class type

查看:346
本文介绍了Delphi在类过程中使用反射来获取动态类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在类过程/函数(静态方法)内的当前类上使用反射。不使用自我关键字怎么办?

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;


推荐答案

您可以使用 ClassInfo GetType

You can use ClassInfo and GetType:

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

这篇关于Delphi在类过程中使用反射来获取动态类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆