Getting the Unit Name which belongs to any type (TRttiType)

微笑、不失礼 提交于 2019-12-03 07:50:15

It doesn't look like there is. The RTTI comes out of the TTypeData structure, which only has a UnitName field explicitly declared for specific types. (This predates D2010 and extended RTTI.) Your #2 looks like the best way to get it, and is probably how a hypothetical TRTTIObject.UnitName would calculate it if they were to put one in.

The information is there but Parsing the Qualified Name is currently the best way to get to it.

If you want to do it the hard way you can by:

In the system.pas unit you have a variable LibModuleList: PLibModule = nil; that contains the list of loaded modules. This is the pointer to the Raw RTTI Information that can be used without RTTI.pas. You could iterate all the raw information get determine the unit name.

The key values of the TLibModule are:

  PLibModule = ^TLibModule;
  TLibModule = record
    Next: PLibModule;  { Linked List of Loaded Modules)
    Instance: LongWord;
    ...
    TypeInfo: PPackageTypeInfo; { List of contained Package Information }
    ...
  end;

Using the TypeInfo: PPackageTypeInfo; you get access to

  PPackageTypeInfo = ^TPackageTypeInfo;
  TPackageTypeInfo = record
    TypeCount: Integer;
    TypeTable: PTypeTable;
    UnitCount: Integer;
    UnitNames: PShortString; { concatenation of Pascal strings, 
                               one for each unit }
  end;

Then there is TypeTable which contains the information to get to PTypeInfo.
sequence.

  TTypeTable = array[0..MaxInt div SizeOf(Pointer) - 1] of Pointer;
  PTypeTable = ^TTypeTable;

An example of how all this works can be found in Rtti.pas TPackage.MakeTypeLookupTable is the key method. This method also shows that QualifiedName always will contain the UnitName. As such your original method of parsing QualfiedName can be depended on.

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