I think you should follow how you yourself can use the serialization methods of the TSomething class.
Here is an example of using XML for serialization:
TSomething = class(TPersistent) protected FNumber: Integer; FLine: String; public procedure WriteToXmlNode(XmlNode: IXmlNode); procedure ReadFromXmlNode(XmlNode: IXmlNode); published property Number: Integer read FNumber write FNumber; property Line: String read FLine write FLine; end; TOther = class(TPersistent) protected FSomething: TSomething; public procedure WriteToXmlNode(XmlNode: IXmlNode); procedure ReadFromXmlNode(XmlNode: IXmlNode); published property Something: TSomething read FSomething write FSomething; end;
In other words, letting each of the classes know how to serialize itself into XML.
Then the TOther write method will look like this:
procedure TOther.WriteToXmlNode(XmlNode: IXmlNode) var ChildNode: IXmlNode; begin // Write something ChildNode := XmlNode.AddChild('Something'); Something.WriteToXmlNode(ChildNode); end;
And the reading method will look like this:
procedure TOther.ReadFromXmlNode(XmlNode: IXmlNode) var ChildNode: IXmlNode; begin // Read ChildNode := XmlNode.ChildNodes.First; while Assigned(ChildNode) do begin // Read something if ChildNode.NodeName = 'Something' then Something.ReadFromXmlNode(ChildNode); // Next child node ChildNode := ChildNode.NextSibling; end; end;
This is a common sense.
I think you will use a similar approach in your example, even if you do not want to serialize XML.
Edit : It was a little incomprehensible what you wanted in your question. If you write components and want the properties of the components to be serialized correctly when you work with them during development, this is not the way to go. The method that I am describing is designed to serialize arbitrary objects at runtime.
source share