/// <summary>
        ///     Adds the item.
        /// </summary>
        /// <param name="index">
        ///     The index.
        /// </param>
        /// <param name="item">
        ///     The item.
        /// </param>
        /// <param name="sourceToTarget">
        ///     if set to <c>true</c> from source to target.
        /// </param>
        private void AddItem(int index, object item, bool sourceToTarget = true)
        {
            ICollectionHandler handler     = sourceToTarget ? this.TargetHandler : this.SourceHandler;
            BindContext        bindContext = sourceToTarget ? this.BindTarget : this.BindSource;
            var list = bindContext.Value as IList;

            bool handled = false;

            if (handler != null)
            {
                handled = handler.AddItem(index, item, bindContext.Source, bindContext.Value);
            }

            if (!handled && list != null)
            {
                IDataGenerator dataGenerator            = sourceToTarget ? this.TargetDataGenerator : this.SourceDataGenerator;
                object         parameter                = sourceToTarget ? this.TargetDataParameter : this.SourceDataParameter;
                Func <object, object, object> generator = sourceToTarget ? this.TargetGenerator : this.SourceGenerator;

                if (dataGenerator != null)
                {
                    list.Insert(index, dataGenerator.Generate(item, parameter));
                }
                else if (generator != null)
                {
                    list.Insert(index, generator(item, parameter));
                }
                else
                {
                    list.Insert(index, item);
                }
            }
        }
Пример #2
0
        void Deserialize(SerializationContext serCtx, object mdata, DataCollection items, object collectionInstance, object position)
        {
            MapData mapData = (mdata != null) ? (MapData)mdata : GetDefaultData();

            foreach (DataNode val in items)
            {
                handler.AddItem(ref collectionInstance, ref position, mapData.ItemType.Deserialize(serCtx, mapData.ItemMapData, val));
            }
            handler.FinishCreation(ref collectionInstance, position);
        }
Пример #3
0
        void Deserialize(SerializationContext serCtx, object obj, DataCollection itemData, DataItem ukwnDataRoot, string baseName)
        {
            Hashtable expandedCollections = null;

            foreach (DataNode value in itemData)
            {
                ItemProperty prop = (ItemProperty)properties [baseName + value.Name];
                if (prop == null)
                {
                    if (value is DataItem)
                    {
                        DataItem root = new DataItem();
                        root.Name        = value.Name;
                        root.UniqueNames = ((DataItem)value).UniqueNames;
                        if (ukwnDataRoot != null)
                        {
                            ukwnDataRoot.ItemData.Add(root);
                        }
                        Deserialize(serCtx, obj, ((DataItem)value).ItemData, root, baseName + value.Name + "/");

                        // If no unknown data has been added, there is no need to keep this
                        // in the unknown items list.
                        if (ukwnDataRoot != null && !root.HasItemData)
                        {
                            ukwnDataRoot.ItemData.Remove(root);
                        }
                    }
                    else if (obj is IExtendedDataItem && (value.Name != "ctype" || baseName.Length > 0))
                    {
                        // store unreadable raw data to a special property so it can be
                        // serialized back an the original format is kept
                        // The ctype attribute don't need to be stored for the root object, since
                        // it is generated by the serializer
                        ukwnDataRoot.ItemData.Add(value);
                    }
                    continue;
                }
                if (prop.WriteOnly || !prop.CanDeserialize(serCtx, obj))
                {
                    continue;
                }

                try
                {
                    if (prop.ExpandedCollection)
                    {
                        ICollectionHandler handler = prop.ExpandedCollectionHandler;
                        if (expandedCollections == null)
                        {
                            expandedCollections = new Hashtable();
                        }

                        object pos, col;
                        if (!expandedCollections.ContainsKey(prop))
                        {
                            col = handler.CreateCollection(out pos, -1);
                        }
                        else
                        {
                            pos = expandedCollections [prop];
                            col = prop.GetValue(obj);
                        }
                        handler.AddItem(ref col, ref pos, prop.Deserialize(serCtx, obj, value));
                        expandedCollections [prop] = pos;
                        prop.SetValue(obj, col);
                    }
                    else
                    {
                        if (prop.HasSetter && prop.DataType.CanCreateInstance)
                        {
                            prop.SetValue(obj, prop.Deserialize(serCtx, obj, value));
                        }
                        else if (prop.DataType.CanReuseInstance)
                        {
                            object pval = prop.GetValue(obj);
                            if (pval == null)
                            {
                                if (prop.HasSetter)
                                {
                                    throw new InvalidOperationException("The property '" + prop.Name + "' is null and a new instance of '" + prop.PropertyType + "' can't be created.");
                                }
                                else
                                {
                                    throw new InvalidOperationException("The property '" + prop.Name + "' is null and it does not have a setter.");
                                }
                            }
                            prop.Deserialize(serCtx, obj, value, pval);
                        }
                        else
                        {
                            throw new InvalidOperationException("The property does not have a setter.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Could not set property '" + prop.Name + "' in type '" + Name + "'", ex);
                }
            }
        }