I'm playing arround with TValue
I've written this code in a blank project:
uses
RTTI;
procedure TForm1.FormCreate(Sender: TObject);
var
s: string;
b: Boolean;
begin
s := TValue.From<Boolean > (True).ToString;
b := TValue.From<string > (s).AsType<Boolean>;
end;
But I can not convert back from string to boolean; I get an Invalid Typecast exception in the second line.
I'm using Delphi XE but it is the same result in Delphi Xe6 which leads me to the conclusion: I'm using TValue wrong.
So please what am I doing wrong.
TValue.ToString()
has specific knowledge of Boolean RTTI and knows to callGetEnumName()
to convert such a value to a string.TValue.AsType()
, on the other hand, does not define anyString
->Enum conversion (see the giantConversions[][]
array inSystem.Rtti.pas
). It only supports Enum conversions when the source and dest types are the same enum type, or when both are Boolean types (they can be different byte sizes). – Splasher