2

If I have a class like this:

TServerSettings = class(TSettings)
strict private
    FHTTPPort : Integer;
published
    property HTTPPort : Integer read FHTTPPort write FHTTPPort default 80;
end;

How can I get the default attribute of the HTTPPort property using RTTI?

4
  • With old or new RTTI ? Commented May 20, 2015 at 14:39
  • Whatever is used in Delphi XE3. Copyright says 1995-2012. Commented May 20, 2015 at 14:40
  • There are both... Just remember my comment and do not blame me when you'll be doing something deeper and the old RTTI won't suffice :-) Commented May 20, 2015 at 14:42
  • 1
    The default you're using doesn't mean what you think it does. It's a storage specifier. It only decides whether or not the property is streamed to the DFM at designtime. (In this case, if the HTTPPort property is 80 when the .DFM is streamed, the HTTPPort property is not saved.) See the Storage Specifiers section: Note: Property values are not automatically initialized to the default value. That is, the default directive controls only when property values are saved to the form file... Commented May 20, 2015 at 14:46

2 Answers 2

4

You can use the Default property of the TRttiInstanceProperty class

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Rtti,
  System.SysUtils;


type
  TServerSettings = class
  strict private
      FHTTPPort : Integer;
  published
      property HTTPPort : Integer read FHTTPPort write FHTTPPort default 80;
  end;

var
   L : TRttiType;
   P : TRttiProperty;
begin
  try
     P:= TRttiContext.Create.GetType(TServerSettings.ClassInfo).GetProperty('HTTPPort');
     if P is TRttiInstanceProperty  then
       Writeln(TRttiInstanceProperty(P).Default);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
Sign up to request clarification or add additional context in comments.

Comments

3

Like this:

{$APPTYPE CONSOLE}

uses
  System.TypInfo;

type
  TMyClass = class
  strict private
    FMyValue: Integer;
  published
    property MyValue: Integer read FMyValue default 42;
  end;

var
  obj: TMyClass;
  PropInfo: PPropInfo;

begin
  obj := TMyClass.Create;
  PropInfo := GetPropInfo(obj, 'MyValue');
  Writeln(PropInfo.Default);
end.

Note that the class as it stands, just as is so for that in your question, is broken. The system will not automatically initialise properties to their default value when an instance is created. You would need to add a constructor to this class to do that.

2 Comments

Thanks that works but what if MyValue is not an integer but a bool or so? Then I get something like incompatible type boolean and integer.
You'll have to cast it. Default property values are only available for ordinal properties. So, for a boolean, 0 means false, 1 means true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.