How to access the private method TStreamReader.FillBuffer in Delphi 10.1 Berlin? [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-11 08:17:35

问题


How to access the private method TStreamReader.FillBuffer in Delphi 10.1 Berlin, we did it with a class helper before 10.1 - but the proposed solution does not work:

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

it fails just because GetMethod returns NIL. Any ideas why this fails?

Edited: I do want to know WHY it fails


回答1:


It fails because the private methods aren't included in this class. See RTTI access to private methods of VCL, e.g. TCustomForm.SetWindowState

There is a workaround for getting the private method though:

See: 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;


来源:https://stackoverflow.com/questions/37874351/how-to-access-the-private-method-tstreamreader-fillbuffer-in-delphi-10-1-berlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!