导航:
论坛 -> DELPHI技术
斑竹:liumazi,sephil
作者:
2020/1/8 19:19:39
标题:
加入我的收藏
楼主:
TTestObject = class private FId: Integer; public property Id: Integer read FId write FId; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var TestObject: TTestObject; LProperty: TRttiProperty; V: TValue; begin Caption := ''; TestObject := TTestObject.Create; TestObject.Id := 100; for LProperty in TRttiContext.Create.GetType(TTestObject).GetProperties do begin V := LProperty.GetValue(TestObject); Caption := Caption + V.AsString; //异常,转换失败. end; TestObject.Free; end; 不知道新版有无此问题.
----------------------------------------------
学无止境
作者:
2020/1/8 20:19:31
1楼:
Caption +string( V.AsVariant); 这样可以不报错 V.AsString; 这样是报 转换错误 XE10.3.3
----------------------------------------------
-
作者:
2020/1/8 20:58:46
2楼:
其它类型也有问题,通用办法换成V.ToString解决.
----------------------------------------------
学无止境
作者:
2020/1/8 22:43:28
3楼:
新版本一样。这个应该不是bug,已经明确告诉你Invalid class typecast,说明类型不对,也就是说,AsString只处理类型是string的那些属性,否则就会报转换错。你需要将任何类型转换到字符串,用ToString比较好。
----------------------------------------------
-广袤璀璨的银河,永无止境的梦想(梦无止境游银河) 博客挂了……