7

我正在使用德尔福 XE4。我尝试为 TBytes 定义一些辅助函数:

TBytesHelper = record helper for TBytes
public
  function GetLength: integer;
end;

function TBytesHelper.GetLength: integer;
begin
  Result := System.Length(Self);
end;

当我尝试使用新的辅助函数时:

var B: TBytes;
    i: integer;
begin
  B := TBytes.Create(1,2,3);
  i := B.GetLength;

  if i <> Length(B) then
    raise Exception.Create('Incorrect result');
end;

我除了结果i3,但事实并非如此。我指的是 SysUtils.pas 中定义的 TStringHelper 具有类似的构造。

有什么我想念的吗?

4

1 回答 1

3

This problem was discussed here: https://forums.embarcadero.com/thread.jspa?threadID=88409 In few words - you can define your own type and use it with record helper:

type
 TMyBytes = array of Byte;

 TBytesHelper = record helper for TMyBytes
   function GetLength: integer;
 end; 

But it doesn't work with TBytes defined in Delphi. Support of helpers for generic types was added recently, so probably it is kind of limitations of current implementation.

于 2013-07-09T16:15:37.730 回答