Delphi 2010 RTTI: use TValue to store data

I would like to be able to use TValue to store data in TList <>. How in:

type TXmlBuilder = class type TXmlAttribute = class Name: String; Value: TValue; // TValue comes from Rtti end; TXmlNode = class Name: String; Parent: TXmlNode; Value: TXmlNode; Attributes: TList<TXmlAttribute>; Nodes: TList<TXmlNode>; function AsString(Indent: Integer): String; end; ... public ... function N(const Name: String): TXmlBuilder; function V(const Value: String): TXmlBuilder; function A(const Name: String; Value: TValue): TXmlBuilder; overload; function A<T>(const Name: String; Value: T): TXmlBuilder; overload; ... end; implementation function TXmlBuilder.A(const Name: String; Value: TValue): TXmlBuilder; var A: TXmlAttribute; begin A := TXmlAttribute.Create; A.Name := Name; A.Value := Value; FCurrent.Attributes.Add(A); Result := Self; end; function TXmlBuilder.A<T>(const Name: String; Value: T): TXmlBuilder; var V: TValue; begin V := TValue.From<T>(Value); A(Name, V); end; 

And a little later, in the main program, I use my "smooth" xml-builder as follows:

 b := TXmlBuilder.Create('root'); bA('attribute', 1).A('other_attribute', 2).A<TDateTime>('third_attribute', Now); 

In the second call, the program throws an access violation exception.

It seems that the first TValue was "released." Can TValue really be used to store Option data at runtime?

I know that options exist in Delphi. My XML constructor will be used to (de) serialize native delphi objects for XML using RTTI, so I will use TValue everywhere.

Yours faithfully,

- Pierre Jager

+3
rtti delphi delphi-2010 tvalue
source share
1 answer

I have found the answer. My mistake.

 function TXmlBuilder.A<T>(const Name: String; Value: T): TXmlBuilder; var V: TValue; begin V := TValue.From<T>(Value); Result := A(Name, V); // I missed the return value end; 

Sorry; -)

+3
source share

All Articles