Exemplo n.º 1
0
        /// <summary>
        /// Fill this data model with a collection reference.
        /// Gets the collection data from the collection and stores it in this serializable model.
        /// </summary>
        public void FromContainer(IInventoryItemContainer container)
        {
            // Serialize based on inventory item serialization model.
            items = new InventoryItemSerializationModel[container.items.Length];
            for (int i = 0; i < container.items.Length; i++)
            {
                var item = container.items[i];
                InventoryItemSerializationModel inst = null;
                if (item != null)
                {
                    var  classes            = ReflectionUtility.GetAllClassesWithAttribute(typeof(SerializationModelAttribute), true);
                    Type serializationModel = typeof(InventoryItemSerializationModel);

                    foreach (var c in classes)
                    {
                        var attrib = (SerializationModelAttribute)c.GetCustomAttributes(typeof(SerializationModelAttribute), true).First();
                        if (c == item.GetType())
                        {
                            DevdogLogger.LogVerbose("Using custom serialization model for " + item.GetType().Name + " - " + attrib.type.Name);
                            serializationModel = attrib.type;
                        }
                    }

                    inst = (InventoryItemSerializationModel)Activator.CreateInstance(serializationModel);
                    inst.FromItem(item);
                }
                else
                {
                    inst = new InventoryItemSerializationModel(null);
                }

                items[i] = inst;
            }
        }
        /// <summary>
        /// Fill this data model with a collection reference.
        /// Gets the collection data from the collection and stores it in this serializable model.
        /// </summary>
        /// <param name="collection"></param>
        public void FromCollection(ItemCollectionBase collection)
        {
            currencies = collection.currenciesGroup.lookups.Select(o => new CurrencyDecoratorSerializationModel(o)).ToArray();
            //            items = collection.items.Select(o => new InventoryItemSerializationModel(o.item)).ToArray();


            // Serialize based on inventory item serialization model.
            items = new InventoryItemSerializationModel[collection.items.Length];
            for (int i = 0; i < collection.items.Length; i++)
            {
                var item = collection.items[i];
                InventoryItemSerializationModel inst = null;
                if (item.item != null)
                {
                    var  classes            = ReflectionUtility.GetAllClassesWithAttribute(typeof(SerializationModelAttribute), true);
                    Type serializationModel = typeof(InventoryItemSerializationModel);

                    foreach (var c in classes)
                    {
                        var attrib = (SerializationModelAttribute)c.GetCustomAttributes(typeof(SerializationModelAttribute), true).First();
                        if (c == item.item.GetType())
                        {
                            DevdogLogger.LogVerbose("Using custom serialization model for " + item.item.GetType().Name + " - " + attrib.type.Name);
                            serializationModel = attrib.type;
                        }
                    }

                    inst = (InventoryItemSerializationModel)Activator.CreateInstance(serializationModel);
                    inst.FromItem(item.item);
                }
                else
                {
                    inst = new InventoryItemSerializationModel(item.item);
                }

                items[i] = inst;
            }
        }