Exemple #1
0
            public override void ReadObject(ref object result, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                if (result == null)
                {
                    throw new Exception("Cannot load an unknown type of dictionary");
                }

                Type keyType   = null;
                Type valueType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    keyType   = GenericArgumentLookup(expectedType, 0);
                    valueType = GenericArgumentLookup(expectedType, 1);
                }

                IDictionary dictionary = result as IDictionary;

                foreach (Xml.XmlDocElement itemElement in xmlElement.GetChildElements("Item"))
                {
                    Xml.XmlDocElement keyElement   = itemElement["Key"];
                    Xml.XmlDocElement valueElement = itemElement["Value"];
                    if (keyElement.Exists && valueElement.Exists)
                    {
                        object key = null;
                        DefaultSerializer.ReadObject(ref key, keyElement, keyType);
                        object value = null;
                        DefaultSerializer.ReadObject(ref value, valueElement, valueType);

                        dictionary.Add(key, value);
                    }
                }
            }
Exemple #2
0
            public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                Type keyType   = null;
                Type valueType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    keyType   = GenericArgumentLookup(expectedType, 0);
                    valueType = GenericArgumentLookup(expectedType, 1);
                    xmlElement.SetAttribute("keyType", keyType.AssemblyQualifiedNameWithoutVersion());
                    xmlElement.SetAttribute("valueType", valueType.AssemblyQualifiedNameWithoutVersion());
                }

                // Make sure the element gets created when there are zero elements in the list
                xmlElement.MakeSureElementExists();
                IDictionary dict = o as System.Collections.IDictionary;

                foreach (DictionaryEntry entry in dict)
                {
                    Xml.XmlDocElement itemElement = xmlElement.AddElement("Item");
                    Xml.XmlDocElement keyElement  = itemElement.AddElement("Key");
                    DefaultSerializer.WriteObject(entry.Key, keyElement, keyType);
                    Xml.XmlDocElement valueElement = itemElement.AddElement("Value");
                    DefaultSerializer.WriteObject(entry.Value, valueElement, valueType);
                }
            }
Exemple #3
0
            public override void ReadObject(ref object result, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                if (result == null)
                {
                    throw new Exception("Cannot load an unknown type of list");
                }

                Type itemType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    itemType = GenericArgumentLookup(expectedType, 0);
                }

                var list = result as IList;

                foreach (Xml.XmlDocElement itemElement in xmlElement.GetChildElements("Item"))
                {
                    object child = null;
                    DefaultSerializer.ReadObject(ref child, itemElement, itemType);
                    if (list != null)
                    {
                        list.Add(child);
                    }
                    else
                    {
                        expectedType.InvokeMember("Add", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, result, new object[] { child });
                    }
                }
            }
Exemple #4
0
            public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                Type itemType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    itemType = GenericArgumentLookup(expectedType, 0);
                }

                // Make sure the element gets created when there are zero elements in the list
                xmlElement.MakeSureElementExists();
                foreach (object childValue in (o as System.Collections.IEnumerable))
                {
                    Xml.XmlDocElement childElement = xmlElement.AddElement("Item");
                    DefaultSerializer.WriteObject(childValue, childElement, itemType);
                }
            }
Exemple #5
0
            public override void ReadObject(ref object result, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                result = null;

                string nullReference = xmlElement.GetAttribute("null");

                if (nullReference == "true")
                {
                    result = null;
                    return;
                }

                string typeString = xmlElement.GetAttribute("type");
                Type   type       = null;

                if ((typeString == null) || (typeString == ""))
                {
                    type = expectedType;
                }
                else
                {
                    type = Type.GetType(typeString);
                }

                if (type == null)
                {
                    throw new Exception("Cannot load an unknown type");
                }

                if (type.IsEnum)
                {
                    result = EnumParse(type, xmlElement.Value);
                    return;
                }

                if (typeof(string) == type)
                {
                    result = xmlElement.Value;
                    return;
                }
                if (typeof(double) == type)
                {
                    //result = double.Parse(xmlElement.Value);
                    result = XmlConvert.ToDouble(xmlElement.Value);
                    return;
                }
                if (typeof(int) == type)
                {
                    //result = int.Parse(xmlElement.Value);
                    result = XmlConvert.ToInt32(xmlElement.Value);
                    return;
                }
                if (typeof(DateTime) == type)
                {
                    DateTime dateTimeResult;
                    if (!DateTime.TryParseExact(xmlElement.Value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeResult))
                    {
                        dateTimeResult = DateTime.Parse(xmlElement.Value);
                    }
                    result = dateTimeResult;
                    return;
                }
                if (typeof(IConvertible).IsAssignableFrom(type))
                {
                    result = Convert.ChangeType(xmlElement.Value, type);
                    return;
                }

                if (type.IsPrimitive)
                {
                    throw new Exception("Primitive that's not convertible");
                }

                if (type.IsArray)
                {
                    Type itemType = type.GetElementType();

                    ArrayList arrayList = new ArrayList();
                    foreach (var childElement in xmlElement.GetChildElements("Item"))
                    {
                        object child = null;
                        ReadObject(ref child, childElement, itemType);
                        arrayList.Add(child);
                    }

                    result = arrayList.ToArray(itemType);
                    return;
                }

                try
                {
                    result = Activator.CreateInstance(type);
                }
                catch (MissingMethodException e)
                {
                    throw new MissingMethodException("No parameterless constructor defined for type " + type.FullName, e);
                }

                foreach (BaseTypeSerializer typeSerializer in _typeSerializers)
                {
                    if (typeSerializer.CanSerializeType(type))
                    {
                        typeSerializer.DefaultSerializer = this;
                        typeSerializer.ReadObject(ref result, xmlElement, type);
                        return;
                    }
                }

                var memberLookup = GetMemberLookup(type);

                foreach (var childElement in xmlElement.GetChildElements())
                {
                    MemberData member;
                    memberLookup.TryGetValue(childElement.Name, out member);
                    if (member == null)
                    {
                        // If there's an entry in the xml that doesn't exist in the class then we'll just ignore it.
                    }
                    else
                    {
                        string selfReference = childElement.GetAttribute("selfReference");
                        if (member.field != null)
                        {
                            var field = member.field;
                            if (selfReference == "true")
                            {
                                field.SetValue(result, result);
                            }
                            else
                            {
                                object childValue = null;
                                ReadObject(ref childValue, childElement, field.FieldType);
                                field.SetValue(result, childValue);
                            }
                        }
                        else if (member.property != null)
                        {
                            var property = member.property;
                            if (selfReference == "true")
                            {
                                property.SetValue(result, result);
                            }
                            else
                            {
                                object childValue = null;
                                ReadObject(ref childValue, childElement, property.PropertyType);
                                property.SetValue(result, childValue);
                            }
                        }
                    }
                }

                for (type = type.BaseType; type != typeof(object); type = type.BaseType)
                {
                    foreach (BaseTypeSerializer typeSerializer in _typeSerializers)
                    {
                        if (typeSerializer.CanSerializeType(type))
                        {
                            typeSerializer.DefaultSerializer = this;
                            typeSerializer.ReadObject(ref result, xmlElement, type);
                            return;
                        }
                    }
                }
            }
Exemple #6
0
            public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                if (o == null)
                {
                    xmlElement.SetAttribute("null", "true");
                    return;
                }

                Type   type     = o.GetType();
                string typeName = null;

                string canRecreateTypeName;

                if (!_canRecreateLookup.TryGetValue(type, out canRecreateTypeName))
                {
                    canRecreateTypeName = typeName;
                    if (canRecreateTypeName == null)
                    {
                        canRecreateTypeName = type.AssemblyQualifiedNameWithoutVersion();
                    }
                    bool canRecreate = (Type.GetType(canRecreateTypeName) != null);
                    if (!canRecreate)
                    {
                        canRecreateTypeName = type.AssemblyQualifiedName;
                        canRecreate         = (Type.GetType(canRecreateTypeName) != null);
                        if (!canRecreate)
                        {
                            canRecreateTypeName = "";
                        }
                    }
                    _canRecreateLookup.Add(type, canRecreateTypeName);
                }
                if (canRecreateTypeName == "")
                {
                    throw new Exception("Could not recreate type from Assembly Qualified Name");
                }

                if (expectedType == null)
                {
                    xmlElement.SetAttribute("type", canRecreateTypeName);
                }

                if (o is DateTime)
                {
                    xmlElement.Value = ((DateTime)o).ToString("yyyy-MM-dd HH:mm:ss");
                    return;
                }

                if (o is IConvertible)
                {
                    xmlElement.Value = Convert.ChangeType(o, typeof(string)) as string;
                    return;
                }

                if (type.IsPrimitive)
                {
                    throw new Exception("Primitive that's not convertible");
                }

                if (type.IsArray)
                {
                    Type itemType = type.GetElementType();

                    Array array = o as Array;
                    foreach (object childValue in array)
                    {
                        Xml.XmlDocElement childElement = xmlElement.AddElement("Item");
                        WriteObject(childValue, childElement, itemType);
                    }
                    return;
                }

                for (; type != typeof(object); type = type.BaseType)
                {
                    foreach (BaseTypeSerializer typeSerializer in _typeSerializers)
                    {
                        if (typeSerializer.CanSerializeType(type))
                        {
                            typeSerializer.DefaultSerializer = this;
                            typeSerializer.WriteObject(o, xmlElement, type);
                            return;
                        }
                    }

                    FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                    foreach (FieldInfo field in fields)
                    {
                        object[] xmlIgnoreAttributes;
                        string   attributesKey = "ig:Field " + field.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = field.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        if (xmlIgnoreAttributes.Length > 0)
                        {
                            continue;
                        }

                        attributesKey = "igEmpty:Field " + field.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = field.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreIfNullOrEmptyAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        bool ignoreIfNullOrEmpty = (xmlIgnoreAttributes.Length > 0);

                        object childValue = field.GetValue(o);
                        if (ignoreIfNullOrEmpty)
                        {
                            if (childValue == null)
                            {
                                continue;
                            }
                            if ((childValue is string) && string.IsNullOrEmpty(childValue as string))
                            {
                                continue;
                            }
                        }

                        Xml.XmlDocElement childElement = xmlElement[field.Name];

                        if (childValue == o)
                        {
                            childElement.SetAttribute("selfReference", "true");
                        }
                        else
                        {
                            WriteObject(childValue, childElement, field.FieldType);
                        }
                    }

                    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                    foreach (PropertyInfo property in properties)
                    {
                        if (!property.CanRead || !property.CanWrite)
                        {
                            continue;
                        }

                        object[] xmlIgnoreAttributes;
                        string   attributesKey = "ig:Property " + property.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        if (xmlIgnoreAttributes.Length > 0)
                        {
                            continue;
                        }

                        attributesKey = "igEmpty:Property " + property.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreIfNullOrEmptyAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        bool ignoreIfNullOrEmpty = (xmlIgnoreAttributes.Length > 0);

                        object childValue = property.GetValue(o, null);
                        if (ignoreIfNullOrEmpty)
                        {
                            if (childValue == null)
                            {
                                continue;
                            }
                            if ((childValue is string) && string.IsNullOrEmpty(childValue as string))
                            {
                                continue;
                            }
                        }

                        Xml.XmlDocElement childElement = xmlElement[property.Name];

                        if (childValue == o)
                        {
                            childElement.SetAttribute("selfReference", "true");
                        }
                        else
                        {
                            WriteObject(childValue, childElement, property.PropertyType);
                        }
                    }
                }
            }