Ejemplo n.º 1
0
        protected object ReadProperties(ES3Reader reader, object obj)
        {
            // Iterate through each property in the file and try to load it using the appropriate
            // ES3Member in the members array.
            foreach (string propertyName in reader.Properties)
            {
                // Find the property.
                ES3Member property = null;
                for (int i = 0; i < members.Length; i++)
                {
                    if (members[i].name == propertyName)
                    {
                        property = members[i];
                        break;
                    }
                }

                // If this is a class which derives directly from Dictionary, we need to load it's dictionary first.
                if (propertyName == "_Values")
                {
                    var baseType = ES3TypeMgr.GetOrCreateES3Type(ES3Reflection.BaseType(obj.GetType()));
                    if (baseType.isDictionary)
                    {
                        var dict   = (IDictionary)obj;
                        var loaded = (IDictionary)baseType.Read <IDictionary>(reader);
                        foreach (DictionaryEntry kvp in loaded)
                        {
                            dict[kvp.Key] = kvp.Value;
                        }
                    }
                    else if (baseType.isCollection)
                    {
                        var loaded = (IEnumerable)baseType.Read <ICollection>(reader);

                        var type = baseType.GetType();

                        if (type == typeof(ES3ListType))
                        {
                            foreach (var item in loaded)
                            {
                                ((IList)obj).Add(item);
                            }
                        }
                        else if (type == typeof(ES3QueueType))
                        {
                            var method = baseType.type.GetMethod("Enqueue");
                            foreach (var item in loaded)
                            {
                                method.Invoke(obj, new object[] { item });
                            }
                        }
                        else if (type == typeof(ES3StackType))
                        {
                            var method = baseType.type.GetMethod("Push");
                            foreach (var item in loaded)
                            {
                                method.Invoke(obj, new object[] { item });
                            }
                        }
                        else if (type == typeof(ES3HashSetType))
                        {
                            var method = baseType.type.GetMethod("Add");
                            foreach (var item in loaded)
                            {
                                method.Invoke(obj, new object[] { item });
                            }
                        }
                    }
                }

                if (property == null)
                {
                    reader.Skip();
                }
                else
                {
                    var type = ES3TypeMgr.GetOrCreateES3Type(property.type);

                    if (ES3Reflection.IsAssignableFrom(typeof(ES3DictionaryType), type.GetType()))
                    {
                        property.reflectedMember.SetValue(obj, ((ES3DictionaryType)type).Read(reader));
                    }
                    else if (ES3Reflection.IsAssignableFrom(typeof(ES3CollectionType), type.GetType()))
                    {
                        property.reflectedMember.SetValue(obj, ((ES3CollectionType)type).Read(reader));
                    }
                    else
                    {
                        object readObj = reader.Read <object>(type);
                        property.reflectedMember.SetValue(obj, readObj);
                    }
                }
            }
            return(obj);
        }