我想获得在特定单元中定义的完整类列表 如何获取这些类的所有实例的列表,无论它们在何处创建? 在回答您的问题之前,请记住在与Rtti相关的问题中始终包含您的delphi版本. 1)您正在使
>如何获取这些类的所有实例的列表,无论它们在何处创建? 在回答您的问题之前,请记住在与Rtti相关的问题中始终包含您的delphi版本.
1)您正在使用新版本的delphi(> = 2010)的Asumming,您可以使用QualifiedName属性获取类型的单元名称,从那里您必须检查IsInstance属性以确定是否是类.
检查下一个样本.
{$APPTYPE CONSOLE}
{$R *.res}
uses
Rtti,
System.SysUtils;
procedure Test;
Var
t : TRttiType;
//extract the unit name from the QualifiedName property
function GetUnitName(lType: TRttiType): string;
begin
Result := StringReplace(lType.QualifiedName, '.' + lType.Name, '',[rfReplaceAll])
end;
begin
//list all the types of the System.SysUtils unit
for t in TRttiContext.Create.GetTypes do
if SameText('System.SysUtils',GetUnitName(t)) and (t.IsInstance) then
Writeln(t.Name);
end;
begin
try
Test;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
2)Rtti无法列出类的实例.因为Rtti是关于类型信息而不是实例.
