如何使用RTTI将TDateTime属性与Double属性区分开? [英] How can I distinguish TDateTime properties from Double properties with RTTI?

查看:173
本文介绍了如何使用RTTI将TDateTime属性与Double属性区分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the RTTI system in Delphi 2010, is there any way to find out if a property is a TDateTime? It's currently treating it as a double whenever I call back asVariant and also if I check the property type. Is this due to the fact it can only see the base type? (TDateTime = double)

推荐答案

尝试检查 Name TRttiProperty.PropertyType

Try checking the Name property of the TRttiProperty.PropertyType

我没有Delphi 2010,但这在XE中有效。

I don't have Delphi 2010, but this works in 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

这篇关于如何使用RTTI将TDateTime属性与Double属性区分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆