使用Delphi 2010中的RTTI系统,有什么方法可以找出属性是否是TDateTime?目前,每当我回调 asVariant 以及检查属性类型时,它都会将其视为双精度。这是因为它只能看到基本类型吗? (TDateTime = 双精度)
尝试检查Name
的财产TRttiProperty.PropertyType http://docwiki.embarcadero.com/VCL/XE2/en/Rtti.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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)