【问题标题】:How to access the private method TStreamReader.FillBuffer in Delphi 10.1 Berlin? [duplicate]如何在 Delphi 10.1 Berlin 中访问私有方法 TStreamReader.FillBuffer? [复制]
【发布时间】:2016-06-17 06:13:00
【问题描述】:

如何在 Delphi 10.1 Berlin 中访问私有方法 TStreamReader.FillBuffer,我们在 10.1 之前使用类助手完成了它 - 但建议的解决方案不起作用:

uses System.Rtti;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Assert(Assigned(TRttiContext.Create.GetType(TStreamReader).GetMethod('FillBuffer')), 
    'Failed');
end;

它失败只是因为 GetMethod 返回 NIL。任何想法为什么会失败?

编辑: 我确实想知道为什么会失败

【问题讨论】:

  • 它可能会失败,因为它没有在启用扩展 RTTI 的情况下编译。
  • 你在这里看到@LURD 的回答了吗:stackoverflow.com/questions/36716363/…
  • @Rudy:我尝试在我的 unit1.pas 中添加 {$METHODINFO ON} 但我认为这应该在 system.classes 中完成(其中声明了 TStreamReader)。
  • @MartynA: TMethod(P).Code := @TStreamReader.FillBuffer 确实有效,谢谢,请添加为答案
  • 很高兴它起作用了,但我认为如果您添加自己的答案会更好,因为我不知道您最终使用了什么代码。

标签: delphi delphi-10.1-berlin class-helpers


【解决方案1】:

它失败了,因为私有方法不包含在这个类中。 见RTTI access to private methods of VCL, e.g. TCustomForm.SetWindowState

虽然有一个解决方法可以获取私有方法:

见:How to access private methods without helpers?

type
  TStreamReaderHelper = class helper for TStreamReader
  public
    procedure FillBuffer(var Encoding: TEncoding);
  end;

procedure TStreamReaderHelper.FillBuffer(var Encoding: TEncoding);
var
  Method: procedure(var Encoding: TEncoding) of object;
begin
  TMethod(Method).Code := @TStreamReader.FillBuffer;
  TMethod(Method).Data := Self;
  Method(Encoding);
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多