加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:15:38 所属栏目:大数据 来源:网络整理
导读:在Delphi 2010中使用RTTI系统,有没有办法找出属性是否是TDateTime?当我回调为Variant时,如果我检查属性类型,它现在将其视为双精度。这是因为它只能看到基本类型吗? (TDateTime = double) 解决方法 尝试检查 TRttiProperty.PropertyType 的Name属性 我没
在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读