How do I use class helpers to access strict private members of a class?

前端 未结 2 816
眼角桃花
眼角桃花 2020-12-05 09:58

This is a follow-up question to: How to hide a protected procedure of an object?
(I\'m a bit fuzzy on the whole class helper concept)

Suppose I have a

2条回答
  •  广开言路
    2020-12-05 10:43

    Access to private and strict private members of a class with class helpers was removed in Delphi 10.1 Berlin. See Closing the Class Helpers Private Access Loophole.

    But there is still an open loophole:

    unit Shy;
    
    interface
    
    type
      TShy = class(TObject)
      strict private
        procedure TopSecret;
      private
        procedure DirtyLaundry;
      protected
        procedure ResistantToChange;
      end;
    
    implementation
    
    procedure TShy.DirtyLaundry;
    begin
      WriteLn('DirtyLaundry');
    end;
    
    procedure TShy.ResistantToChange;
    begin
      WriteLn('ResistantToChange');
    end;
    
    procedure TShy.TopSecret;
    begin
      WriteLn('TopSecret');
    end;
    
    end.
    

    Program TestClassHelpers;
    
    {$APPTYPE CONSOLE}
    
    Uses
      Shy;
    
    type
      TNotShy = class helper for TShy
      public
        procedure LetMeIn;
      end;
    
    procedure TNotShy.LetMeIn;
    var
      P : procedure of object;
    begin
      TMethod(P).Code := @TShy.TopSecret;
      TMethod(P).Data := Self;
      P; // Call TopSecret
      TMethod(P).Code := @TShy.DirtyLaundry;
      TMethod(P).Data := Self;
      P; // Call DirtyLaundry;
      Self.ResistantToChange;  // Protected access works without problems
    end;
    
    var
      myObj: TShy;
    begin
      myObj := TShy.Create;
      try
        myObj.LetMeIn;
        ReadLn;
      finally
        myObj.Free;
      end;
    end.
    

提交回复
热议问题