Access a strict protected property of a Delphi class?

白昼怎懂夜的黑 提交于 2019-11-26 23:03:40

问题


I need to access a strict protected property, because I need to create a validation (based in the value of this property) to avoid a bug. (I don't have the source code of the third party class which has this property) only I have the definition of the class (interface) and the dcu (so I can't change the property visibility). The question is Exist a way to access a strict protected property? (I really read the Hallvard Vassbotn Blog, but I don't find anthing about this particular topic.)


回答1:


This class helper example compiles fine :

type
  TMyOrgClass = class
  strict private
    FMyPrivateProp: Integer;
  strict protected
    property MyProtectedProp: Integer read FMyPrivateProp;
  end;

  TMyClassHelper = class helper for TMyOrgClass
  private
    function GetMyProtectedProp: Integer;
  public
    property MyPublicProp: Integer read GetMyProtectedProp;
  end;

function TMyClassHelper.GetMyProtectedProp: Integer;
begin
  Result:= Self.FMyPrivateProp;  // Access the org class with Self
end;

Some more information about class helpers can be found here : should-class-helpers-be-used-in-developing-new-code

Update

Starting with Delphi 10.1 Berlin, accessing private or strict private members with class helpers does not work. It was considered a compiler bug and has been corrected. Accessing protected or strict protected members is still allowed with class helpers though.

In the above example access to a private member was illustrated. Below shows a working example with access to a strict protected member.

function TMyClassHelper.GetMyProtectedProp: Integer;
begin
  with Self do Result:= MyProtectedProp;  // Access strict protected property
end;



回答2:


You can use a variant of the standard protected hack.

Unit 1

type
  TTest = class
  strict private
    FProp: Integer;
  strict protected
    property Prop: Integer read FProp;
  end;

Unit 2

type
  THackedTest = class(TTest)
  strict private
    function GetProp: Integer;
  public
    property Prop: Integer read GetProp;
  end;

function THackedTest.GetProp: Integer;
begin
  Result := inherited Prop;
end;

Unit 3

var
  T: TTest;

....

THackedTest(T).Prop;

Strict protected only allows you to access the member from the defining class, and subclasses. So you have to actually implement a method on the cracking class, make it public, and use that method as the route into the target strict protected member.



来源:https://stackoverflow.com/questions/8330003/access-a-strict-protected-property-of-a-delphi-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!