Necesito obtener el nombre de la unidad (espacio de nombres) de cualquier TRttiType
.Obtener el nombre de la unidad que pertenece a cualquier tipo (TRttiType)
Hasta ahora, he intentado lo siguiente.
1) usando el PTypeData.UnitName
, esta solución funciona, pero solo cuando el TTypeKind es tkClass.
procedure ListAllUnits;
var
ctx : TRttiContext;
lType: TRttiType;
Units: TStrings;
begin
Units:=TStringList.Create;
try
ctx := TRttiContext.Create;
for lType in ctx.GetTypes do
if lType.IsInstance then //only works for classes
if Units.IndexOf(UTF8ToString(GetTypeData(lType.Handle).UnitName))<0 then
Units.Add(UTF8ToString(GetTypeData(lType.Handle).UnitName));
Writeln(Units.Text);
finally
Units.Free;
end;
end;
2) Analizar la propiedad QualifiedName
, esta solución funciona bien hasta ahora, pero no estoy muy contento con él.
procedure ListAllUnits2;
function GetUnitName(lType: TRttiType): string;
begin
Result := StringReplace(lType.QualifiedName, '.' + lType.Name, '',[rfReplaceAll])
end;
var
ctx: TRttiContext;
lType: TRttiType;
Units: TStrings;
begin
Units := TStringList.Create;
try
ctx := TRttiContext.Create;
for lType in ctx.GetTypes do
if Units.IndexOf(GetUnitName(lType)) < 0 then
Units.Add(GetUnitName(lType));
Writeln(Units.Text);
finally
Units.Free;
end;
end;
la pregunta es, existe otra forma confiable de obtener el nombre de la unidad de cualquier TRttiType
?