Undocumented Members of TPropInfo

2019-02-17 17:17发布

问题:

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.