Delphi 如何使用增强的RTTI获取从给定类派生的类列表?

Delphi 如何使用增强的RTTI获取从给定类派生的类列表?,delphi,rtti,Delphi,Rtti,我需要获取表单类型的列表,但仅限于从给定基表单派生的类型 我使用Delphi2010和增强的RTTI浏览类型 我目前的代码是: rc := TRTTIContext.Create; rtyps := rc.GetTypes; for rtyp in rtyps do begin if not(rtyp.IsInstance) then Continue; // Now I need to check if rtyp.AsInstance.MetaclassType is derived

我需要获取表单类型的列表,但仅限于从给定基表单派生的类型

我使用Delphi2010和增强的RTTI浏览类型

我目前的代码是:

rc := TRTTIContext.Create;
rtyps := rc.GetTypes;
for rtyp in rtyps do
begin
  if not(rtyp.IsInstance) then Continue;

  // Now I need to check if rtyp.AsInstance.MetaclassType is derived from TMyBaseForm
end;
我不想实例化对象并使用“is”操作符,因为它不会及时执行。
作为当前的解决方法,我测试在RTTI上下文中是否找到了TMyBaseForm中引入的方法:

if (rtyp.GetMethod('MyMethod') = nil) then Continue;
但这不是一个干净的解决方案,因为如果在另一个类分支中引入了同名的方法,可能会导致问题

那么,我的问题是:是否有一种常规的方法来检测一个类类型是否派生自另一个类类型


谢谢,

当您调用
AsInstance
时返回一个,从那里您必须访问属性,该属性是对反射类型的引用,最后使用TClass您可以调用该函数


我不知道如何使用RTTI实现它,但是有一个函数。它返回类的直接祖先的类型。或者,如果您需要确定某个特定的类类型或对象是某个类的实例还是其子类的实例,请使用函数。
for rtyp in rtyps do
if (rtyp.TypeKind=tkClass) and rtyp.IsInstance and rtyp.AsInstance.MetaclassType.InheritsFrom(TMyBaseForm) then
begin

  // do something
end;