【问题标题】:Undocumented Members of TPropInfoTPropInfo 的无证成员
【发布时间】:2025-12-05 15:45:02
【问题描述】:

System.TypInfo.TPropInfo 有两个函数成员(至少在 D-XE3 中):

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

我找不到它们的任何文档或使用它们的任何示例。它们的用途是什么以及如何使用它们? (希望这是一个问题。)

【问题讨论】:

    标签: delphi delphi-xe3


    【解决方案1】:

    NameFld 函数将属性名称作为TTypeInfoFieldAccessor 返回。

    这允许您执行以下操作:

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

    TTypeInfoFieldAccessor 在内部将属性名称存储在 shortstring 中。
    因为 NextGen 编译器不支持短字符串,所以使用 PByte 类型。
    (我猜作者不想在源代码中乱扔 ifdef 并撕掉 PShortstring 引用)

    Tail 的输入是一个指向内部短字符串长度字段的 PByte。

    这是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;
    

    因为短字符串不是以空字符结尾的,所以不能执行简单的“循环直到找到空字符”类型的循环。
    因此,可以使用从头到尾的循环将短字符串转换为普通字符串。
    奇怪的是,在实际的 RTL 源代码中,长度字节无处不在,而不是 tail 函数;所以它看起来像一个剩菜。
    包含size 函数并删除tail 会更有意义。

    【讨论】:

      最近更新 更多