Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I publish a private class function of an RTL unit?

Tags:

delphi

Delphi XE3, System.Rtti.pas

I need to access two private class functions, but I have read that if I modify the interface section of an RTL unit then I need to recompile all the RTL. Not for the faint heart!

The two private class functions are in System.Rtti.pas:

    class function GetName<T{: enum}>(AValue: T): string; reintroduce; static;
    class function GetValue<T{: enum}>(const AName: string): T; static;

System.Rtti.pas

  TRttiEnumerationType = class(TRttiOrdinalType)
  private
    function GetMaxValue: Longint; override;
    function GetMinValue: Longint; override;
    function GetUnderlyingType: TRttiType;
    constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
    {$HINTS OFF}
    function GetNames: TArray<string>;
    class function GetName<T{: enum}>(AValue: T): string; reintroduce; static;
    class function GetValue<T{: enum}>(const AName: string): T; static;
    {$HINTS ON}
  public
    property UnderlyingType: TRttiType read GetUnderlyingType;
  end;
like image 398
Fabio Vitale Avatar asked Aug 01 '15 05:08

Fabio Vitale


2 Answers

You can also access the private class methods with a class helper.

program Project50;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,RTTI;

Type
  TRttiEnumerationTypeHelper = class helper for TRttiEnumerationType
  public
    class function Name<T>(AValue: T): string; inline;
    class function Value<T>(const AName: string): T; inline;
  end;

class function TRttiEnumerationTypeHelper.Name<T>(AValue: T): string;
begin
  Result := TRttiEnumerationType.GetName<T>(AValue);
end;

class function TRttiEnumerationTypeHelper.Value<T>(const AName: string): T;
begin
  Result := TRttiEnumerationType.GetValue<T>(AName);
end;

Type
  TEnum = (teTest1,teTest2,teTest3);

begin
  WriteLn( TRttiEnumerationType.Name<TEnum>(teTest1));
  WriteLn( Ord(TRttiEnumerationType.Value<TEnum>('teTest1')));
  ReadLn;
end.

It has the drawback that another helper may hide this declaration. To use it, just put the declaration in a unit and include the unit wherever you need it.

If you want to have the original names of the functions, use the trick described here: Can I call static private class method with class helper?

like image 83
LU RD Avatar answered Oct 23 '22 02:10

LU RD


Your options include:

  1. Recompiling the entire RTL.
  2. Using RTTI to access the private methods.
  3. Adding a new class to the RTTI unit that exposes the functions. As I recall, adding types or functions to the interface section won't force a total recompile of the RTL.
  4. Implementing the functionality yourself, outside the RTTI unit. For instance see the answers here: Generic functions for converting an enumeration to string and back.
  5. Use a class helper to crack the privates as described in LURD's answer.
like image 2
David Heffernan Avatar answered Oct 23 '22 03:10

David Heffernan