2010. május 8., szombat

Use RTTI to determine if a property is a TDateTime


Problem/Question/Abstract:

How to use RTTI to determine if a property is a TDateTime

Answer:

When it comes to RTTI, TDateTime and Double are not the same. That's what the extra "type" keyword in TDateTime's declaration is for: to give it its own RTTI, distinct from that of Double. Here is an example:

program Test;

uses
  TypInfo;

{$APPTYPE CONSOLE}
{$M+}
type
  TTest = class
  private
    FDateTime: TDateTime;
  published
    property D: TDateTime read FDateTime write FDateTime;
  end;

var
  T: TTest;
  DateInfo: Pointer;
  TestInfo: PPropInfo;
begin
  T := TTest.Create;
  DateInfo := TypeInfo(TDateTime);
  TestInfo := GetPropInfo(T, 'D');
  writeln(DateInfo = TestInfo^.PropType^);
  readln;
end.

It should print TRUE on the console.

Nincsenek megjegyzések:

Megjegyzés küldése