delphi – 如何使用RTTI区分TDateTime属性和Double属性?

在Delphi 2010中使用RTTI系统,有没有办法找出属性是否是TDateTime?当我回调为Variant时,如果我检查属性类型,它现在将其视为双精度。这是因为它只能看到基本类型吗? (TDateTime = double)

解决方法

尝试检查 TRttiProperty.PropertyType的Name属性

我没有德尔福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

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...