Undocumented Members of TPropInfo

空扰寡人 提交于 2019-12-04 01:27:08

问题


System.TypInfo.TPropInfo has two function members (at least in D-XE3):

function NameFld: TTypeInfoFieldAccessor; inline;
function Tail: PPropInfo; inline;

I cannot find any documentation for them or any examples of their use. What are they for and how can they be used? (Hope that qualifies as one question.)


回答1:


The NameFld function returns the name of a property as a TTypeInfoFieldAccessor.

This allows you to do the following:

MyPropertyName:= MyPropInfo.NameFld.ToString;
if (PropInfoA.NameFld = PropInfoB.NameFld) then begin 
  writeln('property names are the same');
end;

The TTypeInfoFieldAccessor stores the name of a property in a shortstring internally.
Because the NextGen compiler does not support shortstrings, a PByte type is used.
(I guess the author did not want to litter the source with ifdefs and ripped out the PShortstring references)

The input of Tail is a PByte pointing to length field of the internal shortstring.

Here's the source code for tail.

function TTypeInfoFieldAccessor.Tail: PByte;
begin
  Result:= 
    FData    //Start of the shortstring 
    + FData^ + //Length of the stringData
    + 1; //Add one for the length byte itself
end;

Because shortstrings are not null terminated, you cannot do a simple "loop until the null char is found" kind of loop.
Therefore a loop from start to tail can employed to transfer the shortstring into a normal string.
Strangely enough in the actual RTL sourcecode the length byte is used everywhere instead of the tail function; so it looks like a leftover.
It would have made more sense to include a size function and rip out the tail.



来源:https://stackoverflow.com/questions/15832975/undocumented-members-of-tpropinfo

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