在Delphi2010中使用RTTI系统,有没有办法确定一个属性是否是TDateTime?当我回调asVariant和检查属性类型时,它当前将其视为double。这是因为它只能看到基类型吗?(TDateTime =双精度)
发布于 2011-10-20 21:58:51
尝试检查TRttiProperty.PropertyType
的Name
属性
我没有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
https://stackoverflow.com/questions/7836880
复制相似问题