How to access private methods without helpers?

前端 未结 6 2060
孤独总比滥情好
孤独总比滥情好 2020-11-28 06:39

In Delphi 10 Seattle I could use the following code to work around overly strict visibility restrictions.

How do I get access to private variables?



        
6条回答
  •  庸人自扰
    2020-11-28 07:08

    Just use 'with' statement to access private fields !

    See the sample code below, taken from this article I noticed today. (Thanks, Mr.DEKO as always !)

    This hack is originally reported on QualityPortal in August 2019 as described on above aritcle. (login required)


    before rewrite (using "asm" method)

    function TPropertyEditorHelper.GetPropList: PInstPropList;
    {$IF CompilerVersion < 31.0}
    begin
      Result := Self.FPropList;
    end;
    {$ELSE}
    // http://d.hatena.ne.jp/tales/20160420/1461081751
    asm
      MOV EAX, Self.FPropList;
    end;
    {$IFEND}
    

    rewrite using 'with'

    function TPropertyEditorHelper.GetPropList: PInstPropList;
    begin
      with Self do
        Result := FPropList;
    end;
    

    I was amazed it's so simple :-)

提交回复
热议问题