Undocumented Members of TPropInfo

后端 未结 1 1491
北恋
北恋 2021-01-13 03:51

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

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


        
相关标签:
1条回答
  • 2021-01-13 04:36

    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.

    0 讨论(0)
提交回复
热议问题