///////////////////////////////////////////////////////////////////////////

        public bool Serialize <T>(string tagName, ref List <T> value, ISerializablePolymorphicFactory factory) where T : ISerializablePolymorphic
        {
            TNodeID nodeID = TagNameToNodeID(tagName);

            if (GetState() == State.Saving)
            {
                WriteBeginParent(nodeID);

                int elemCount = (value == null) ? 0 : value.Count;
                Serialize("_ElemCount", ref elemCount, 0);

                for (int e = 0; e < elemCount; ++e)
                {
                    T elem = value[e];

                    Serialize("_Elem", ref elem, factory);
                }

                WriteEndParent();

                return(true);
            }
            else
            {
                NodeType nodeType;
                if (!ReadBeginParent(nodeID, out nodeType))
                {
                    value = new List <T>();
                    return(false);
                }

                int elemCount = 0;
                Serialize("_ElemCount", ref elemCount, 0);

                if (value == null)
                {
                    value = new List <T>();
                }
                else
                {
                    value.Clear();
                }

                value.Capacity = elemCount;

                for (int e = 0; e < elemCount; ++e)
                {
                    T loadedElem = default(T);

                    Serialize("_Elem", ref loadedElem, factory);
                    value.Add(loadedElem);
                }

                ReadEndParent();

                return(true);
            }
        }
        ///////////////////////////////////////////////////////////////////////////
        // ISerializablePolymorphic
        ///////////////////////////////////////////////////////////////////////////

        public bool Serialize <T>(string tagName, ref T value, ISerializablePolymorphicFactory factory) where T : ISerializablePolymorphic
        {
            TNodeID nodeID = TagNameToNodeID(tagName);

            if (GetState() == State.Saving)
            {
                if (value == null)
                {
                    WriteNode(nodeID, TAG_SIZE_TOTAL, NodeType.LeafNode);
                }
                else
                {
                    WriteBeginParent(nodeID);

                    int typeEnum = value.GetTypeEnum();
                    Serialize("_TypeID", ref typeEnum, -1);

                    value.Serialize(this);
                    WriteEndParent();
                }

                return(true);
            }
            else
            {
                TNodeSize nodeSize;
                NodeType  nodeType;
                int       nodeStreamPos = FindNodePos(nodeID, out nodeSize, out nodeType);

                bool foundNode = (nodeStreamPos != -1);

                if (!foundNode || nodeType == NodeType.LeafNode)
                {
                    // null or not found
                    value = default(T);
                    GotoStreamPos(nodeStreamPos + nodeSize, true);
                    return(foundNode);
                }

                ReadBeginParentAtPos(nodeStreamPos, nodeSize);

                int  typeEnum     = -1;
                bool hasValidType = Serialize("_TypeID", ref typeEnum, -1);

                if (hasValidType)
                {
                    ISerializablePolymorphic newSerializable = factory.CreateSerializable(typeEnum);

                    try
                    {
                        value = (T)newSerializable;
                    }
                    catch (System.InvalidCastException)
                    {
                        Debug.LogError("Could not cast Factories " +
                                       (newSerializable == null ? "[null]" : newSerializable.GetType().ToString()) +
                                       " to " + typeof(T) + ". EnumValue: " + typeEnum);
                        value = default(T);
                    }

                    if (value == null)
                    {
                        Debug.Assert(false, "Could not Create " + typeof(T) + " via Factory " + factory.ToString());
                        hasValidType = false;
                    }
                    else
                    {
                        value.Serialize(this);
                    }
                }

                ReadEndParent();

                if (!hasValidType)
                {
                    value = default(T);
                    return(false);
                }

                return(hasValidType);
            }
        }