Подтвердить что ты не робот

Как отличить свойства TDateTime от двойных свойств с RTTI?

Используя RTTI-систему в Delphi 2010, есть ли способ узнать, является ли свойство TDateTime? В настоящее время он обрабатывает его как double, когда я перезвоняю asVariant, а также проверяю тип свойства. Это связано с тем, что он может видеть только базовый тип? (TDateTime = double)

4b9b3361

Ответ 1

Попробуйте проверить свойство Name TRttiProperty.PropertyType

У меня нет Delphi 2010, но это работает в XE.

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  Rtti;

type
  TMyClass =class
  private
    FDate: TDateTime;
    FProp: Integer;
    FDate2: TDateTime;
    FDate1: TDateTime;
  public
   property Date1 : TDateTime read FDate1  Write FDate1;
   property Prop : Integer read FProp  Write FProp;
   property Date2 : TDateTime read FDate2  Write FDate2;
  end;

var
 ctx : TRttiContext;
 t :  TRttiType;
 p :  TRttiProperty;
begin
 ctx := TRttiContext.Create;
 try
   t := ctx.GetType(TMyClass.ClassInfo);
   for p in  t.GetProperties do
    if CompareText('TDateTime',p.PropertyType.Name)=0 then
     Writeln(Format('the property %s is %s',[p.Name,p.PropertyType.Name]));
 finally
   ctx.Free;
 end;
  Readln;
end.

этот код возвращает

the property Date1 is TDateTime
the property Date2 is TDateTime

Ответ 2

Здесь ключевой момент при определении типа - это директива типа. Эти два определения различны:

Type
  TDateTime = Double; // here p.PropertyType.Name returns Double

but

Type
  TDateTime = type Double; // here p.PropertyType.Name returns TDateTime

or 

Type
  u8 = type Byte; // here p.PropertyType.Name returns u8

but

Type
  u8 = Byte; // here p.PropertyType.Name returns Byte !