示例#1
0
        bool SerializeBuiltIn(XNode root, string name, object obj)
        {
            var node = new XNode(root, name);

            if (!BuiltInSerializatorX.Serialize(node, obj))
            {
                return(false);
            }

            root.Nodes.Add(node);
            return(true);
        }
示例#2
0
        object Deserialize(XNode node, object defaultValue, Type asType)
        {
            if (node != null)
            {
                string typeName = node.Attribute("XSerializer.SerializedType");
                Type   type     = string.IsNullOrEmpty(typeName) ? null : Type.GetType(typeName, true);

                if (type == null)
                {
                    object enumValue;
                    if (BuiltInSerializatorX.DeserializeEnum(node, asType, out enumValue))
                    {
                        return(enumValue);
                    }

                    object value = BuiltInSerializatorX.Deserialize(node);

                    if (value == null)
                    {
                        return(defaultValue);
                    }

                    return(value);
                }

                object obj = Activator.CreateInstance(type);

                DeserializeProperties(obj, node);
                DeserializeFields(obj, node);

                if (obj is IXSerializable)
                {
                    XNode serializableNode = node.Nodes.Find(n => n.Tag == "IXSerializable");

                    if (serializableNode != null)
                    {
                        IXSerializable serializable = obj as IXSerializable;
                        serializable.Deserialize(serializableNode);
                    }
                }

                return(obj);
            }

            return(defaultValue);
        }