关于Delphi Rtti对应TDatetime的一些启示

测试代码如下:

 1 program Project1;
 2 
 3 {$APPTYPE CONSOLE}
 4 
 5 {$R *.res}
 6 
 7 uses
 8   System.SysUtils,
 9   rtti;
10 
11 type
12   TTest = class
13   private
14     FDateTime: TDateTime;
15   published
16     property D: TDateTime read FDateTime write FDateTime;
17   end;
18 
19 var
20   T: TTest;
21   AValue, rValue: TValue;
22   Context: TRttiContext;
23   rType: TRttiType;
24   rProp: TRttiProperty;
25 begin
26   try
27     T := TTest.Create;
28     T.D := Now;
29     rType := Context.GetType(TTest);
30     rProp := rType.GetProperty('D');
31     Writeln(rProp.PropertyType.Handle.Name);
32     rValue := rProp.GetValue(T);
33     Writeln(rValue.TypeInfo.Name);
34     AValue := TValue.From<TDateTime>(Now);
35     Writeln(AValue.TypeInfo.Name);
36     AValue := Now;
37     Writeln(AValue.TypeInfo.Name);
38     Readln;
39   except
40     on E: Exception do
41       Writeln(E.ClassName, ': ', E.Message);
42   end;
43 end.

运行结果:

TDatetime

TDatetime

TDatetime

Extended

 

一见自明!

你可能感兴趣的:(DateTime)