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;
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;
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?
Your options include:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With