delphidelphi-10.1-berlinclass-helpers

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


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


Solution

  • 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;