如何在Delphi 10.1 Berlin中访问私有方法TStreamReader.FillBuffer? [英] How to access the private method TStreamReader.FillBuffer in Delphi 10.1 Berlin?

查看:75
本文介绍了如何在Delphi 10.1 Berlin中访问私有方法TStreamReader.FillBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

它仅由于GetMethod返回NIL而失败.为什么失败的任何想法?

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

我想知道为什么会失败

推荐答案

它失败,因为该类未包含私有方法.请参见对VCL私有方法的RTTI访问,例如TCustomForm.SetWindowState

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:

请参阅:如何在没有助手的情况下访问私有方法?/a>

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;

这篇关于如何在Delphi 10.1 Berlin中访问私有方法TStreamReader.FillBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆