17

バグを回避するために (このプロパティの値に基づいて) 検証を作成する必要があるため、厳密に保護されたプロパティにアクセスする必要があります。(このプロパティを持つサードパーティ クラスのソース コードはありません) クラス (インターフェイス) と dcu の定義しかありません (そのため、プロパティの可視性を変更することはできません)。問題は、厳密に保護されたプロパティにアクセスする方法はありますか? ( Hallvard Vassbotn ブログを実際に読んでいますが、この特定のトピックについては何も見つかりません。)

4

2 に答える 2

23

このクラスヘルパーの例は正常にコンパイルされます:

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;

クラスヘルパーに関する詳細については、こちらをご覧ください:should-class-helpers-be-used-in-developing-new-code

アップデート

Delphi 10.1ベルリン以降、クラスヘルパーへのアクセスprivateまたはstrict privateメンバーは機能しません。これはコンパイラのバグと見なされ、修正されました。ただし、クラスヘルパーを使用した場合でも、アクセスprotectedまたはメンバーは許可されます。strict protected

上記の例では、プライベートメンバーへのアクセスが示されています。以下に、厳密に保護されたメンバーにアクセスできる実際の例を示します。

function TMyClassHelper.GetMyProtectedProp: Integer;
begin
  with Self do Result:= MyProtectedProp;  // Access strict protected property
end;
于 2011-11-30T17:56:48.537 に答える
16

protected標準のハックの変形を使用できます。

ユニット1

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

ユニット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;

ユニット3

var
  T: TTest;

....

THackedTest(T).Prop;

厳密に保護されているため、定義しているクラスとサブクラスからのみメンバーにアクセスできます。したがって、実際にクラッキングクラスにメソッドを実装して公開し、そのメソッドをターゲットの厳密に保護されたメンバーへのルートとして使用する必要があります。

于 2011-11-30T17:16:12.713 に答える