How to access the private method TStreamReader.Fil

2019-07-24 22:32发布

This question already has an answer here:

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条回答
Melony?
2楼-- · 2019-07-24 22:57

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;
查看更多
登录 后发表回答