Exemplo n.º 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 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 });
                    }
                }
            }
Exemplo n.º 2
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);
                    }
                }
            }
Exemplo n.º 3
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;
                        }
                    }
                }
            }