コード例 #1
0
        public override void Serialize(object data, XmlWriter writer, RoyalXmlSerializer serializer)
        {
            Type   keyValueType = data.GetType();
            object key = keyValueType.GetProperty("Key").GetValue(data), value = keyValueType.GetProperty("Value").GetValue(data);

            writer.WriteStartElement("Key");
            if (key is null)
            {
                writer.WriteAttributeString("Type", "null");
            }
            else
            {
                writer.WriteAttributeString("Type", key.GetType().AssemblyQualifiedName);
                WriteObjectXml(key, writer, serializer);
            }
            writer.WriteEndElement();
            writer.WriteStartElement("Value");
            if (value is null)
            {
                writer.WriteAttributeString("Type", "null");
            }
            else
            {
                writer.WriteAttributeString("Type", value.GetType().AssemblyQualifiedName);
                WriteObjectXml(value, writer, serializer);
            }
            writer.WriteEndElement();
        }
コード例 #2
0
        private void SerializeArray(object data, XmlWriter writer, RoyalXmlSerializer serializer)
        {
            Type  arrayType = data.GetType().GetElementType();
            Array dataArray = data as Array;

            int[] rank = GetRanks(dataArray);
            for (int i = 0; i < rank.Length; i++)
            {
                writer.WriteAttributeString("RankSize" + i, rank[i].ToString());
            }
            int arraySize = Product(i => rank[i], 0, rank.Length - 1);

            for (int i = 0; i < arraySize; i++)
            {
                writer.WriteStartElement("Element");
                object subObject = dataArray.GetValue(OneDimensionToLDimensions(i, rank));
                if (subObject is null)
                {
                    writer.WriteAttributeString("Type", "null");
                }
                else
                {
                    writer.WriteAttributeString("Type", subObject.GetType().AssemblyQualifiedName);
                    WriteObjectXml(subObject, writer, serializer);
                }
                writer.WriteEndElement();
            }
        }
コード例 #3
0
        private object DeserializeArray(XElement node, Type type, RoyalXmlSerializer serializer)
        {
            IEnumerable <XElement> subElements = node.Elements();
            int rankSize = type.GetArrayRank();

            int[] rank = new int[rankSize];
            for (int i = 0; i < rank.Length; i++)
            {
                rank[i] = int.Parse(node.Attribute("RankSize" + i).Value);
            }
            Array toReturn  = Array.CreateInstance(type.GetElementType(), rank);
            int   totalSize = Product(i => rank[i], 0, rank.Length - 1);

            for (int i = 0; i < totalSize; i++)
            {
                XElement elementNode = subElements.ElementAt(i);
                string   typeString  = elementNode.Attribute("Type").Value;
                object   toSet       = null;
                if (typeString != "null")
                {
                    toSet = ReadObjectXml(elementNode, Type.GetType(typeString), serializer);
                }
                toReturn.SetValue(toSet, OneDimensionToLDimensions(i, rank));
            }
            return(toReturn);
        }
コード例 #4
0
        public override object Deserialize(XElement node, Type type, RoyalXmlSerializer serializer)
        {
            Type collectionType = type.GetInterfaces().Where(generic => generic.IsGenericType && generic.GetGenericTypeDefinition() == typeof(ICollection <>)).Single();

            if (typeof(Array).IsAssignableFrom(type))
            {
                return(DeserializeArray(node, type, serializer));
            }
            else
            {
                object     collection            = Activator.CreateInstance(type);
                MethodInfo addToCollectionMethod = collectionType.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
                foreach (XElement subnode in node.Nodes())
                {
                    string subType = subnode.Attribute("Type").Value;
                    if (subType == "null")
                    {
                        addToCollectionMethod.Invoke(collection, new object[] { null });
                    }
                    else
                    {
                        addToCollectionMethod.Invoke(collection, new object[] { ReadObjectXml(subnode, Type.GetType(subType), serializer) });
                    }
                }
                return(collection);
            }
        }
コード例 #5
0
        public override object Deserialize(XElement node, Type type, RoyalXmlSerializer serializer)
        {
            IXmlSerializable toReturn = (IXmlSerializable)Activator.CreateInstance(type);

            using (XmlReader reader = node.CreateReader())
            {
                toReturn.ReadXml(reader);
                return(toReturn);
            }
        }
コード例 #6
0
        public override object Deserialize(XElement node, Type type, RoyalXmlSerializer serializer)
        {
            object toReturn = Activator.CreateInstance(type);

            foreach (XElement field in node.Elements())
            {
                string typeString = field.Attribute("Type").Value;
                type.GetField(field.Name.LocalName, BindingFlags.Public | BindingFlags.Instance)
                .SetValue(toReturn, typeString == "null" ? null : ReadObjectXml(field, Type.GetType(typeString), serializer));
            }
            return(toReturn);
        }
コード例 #7
0
        private object GetChildObject(XElement node, RoyalXmlSerializer serializer)
        {
            string type = node.Attribute("Type").Value;

            if (type == "null")
            {
                return(null);
            }
            else
            {
                return(ReadObjectXml(node, Type.GetType(type), serializer));
            }
        }
コード例 #8
0
 public override void Serialize(object data, XmlWriter writer, RoyalXmlSerializer serializer)
 {
     foreach (FieldInfo info in data.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
     {
         writer.WriteStartElement(info.Name);
         object value = info.GetValue(data);
         if (value is null)
         {
             writer.WriteAttributeString("Type", "null");
         }
         else
         {
             writer.WriteAttributeString("Type", value.GetType().AssemblyQualifiedName);
             WriteObjectXml(value, writer, serializer);
         }
         writer.WriteEndElement();
     }
 }
コード例 #9
0
 public override void Serialize(object data, XmlWriter writer, RoyalXmlSerializer serializer)
 {
     if (data is Array)
     {
         SerializeArray(data, writer, serializer);
     }
     else
     {
         foreach (object o in (IEnumerable)data)
         {
             writer.WriteStartElement("ICollectionElement");
             if (o is null)
             {
                 writer.WriteAttributeString("Type", "null");
             }
             else
             {
                 writer.WriteAttributeString("Type", o.GetType().AssemblyQualifiedName);
                 WriteObjectXml(o, writer, serializer);
             }
             writer.WriteEndElement();
         }
     }
 }
コード例 #10
0
 public override object Deserialize(XElement node, Type type, RoyalXmlSerializer serializer)
 {
     return(char.Parse(node.Value));
 }
コード例 #11
0
 public override void Serialize(object data, XmlWriter writer, RoyalXmlSerializer serializer)
 {
     writer.WriteString(data.ToString());
 }
コード例 #12
0
 public override void Serialize(object data, XmlWriter writer, RoyalXmlSerializer serializer)
 {
     writer.WriteString(((DateTimeOffset)data).ToString(USCultureInfo));
 }
コード例 #13
0
 public override object Deserialize(XElement node, Type type, RoyalXmlSerializer serializer)
 {
     return(DateTimeOffset.Parse(node.Value, USCultureInfo));
 }
コード例 #14
0
        public override object Deserialize(XElement node, Type type, RoyalXmlSerializer serializer)
        {
            object key = GetChildObject(node.Element("Key"), serializer), value = GetChildObject(node.Element("Value"), serializer);

            return(Activator.CreateInstance(type, key, value));
        }
コード例 #15
0
        public override void Serialize(object data, XmlWriter writer, RoyalXmlSerializer serializer)
        {
            IXmlSerializable serializable = (IXmlSerializable)data;

            serializable.WriteXml(writer);
        }