Ejemplo n.º 1
0
        /// <summary>
        /// Assign items to a given list
        /// </summary>
        /// <param name="list">List to assign to</param>
        /// <param name="itemType">Item type</param>
        /// <param name="from">List to assign from</param>
        public static void Assign(IList list, Type itemType, IList from)
        {
            // Assigning null values is not supported
            if (from == null)
            {
                throw new ArgumentNullException(nameof(from));
            }

            // Delete obsolete items
            for (int i = list.Count; i > from.Count; i--)
            {
                list.RemoveAt(i - 1);
            }

            // Update common items
            if (itemType.IsSubclassOf(typeof(ModelObject)))
            {
                for (int i = 0; i < Math.Min(list.Count, from.Count); i++)
                {
                    ModelObject item       = (ModelObject)list[i];
                    ModelObject sourceItem = (ModelObject)from[i];
                    if (item == null || sourceItem == null)
                    {
                        list[i] = sourceItem;
                    }
                    else if (sourceItem.GetType() != item.GetType())
                    {
                        list[i] = sourceItem.Clone();
                    }
                    else
                    {
                        item.Assign(sourceItem);
                    }
                }
            }
            else if (itemType.IsArray)
            {
                for (int i = 0; i < Math.Min(list.Count, from.Count); i++)
                {
                    if (list[i] == null || from[i] == null)
                    {
                        list[i] = from[i];
                    }
                    else
                    {
                        IList listItem = (IList)list[i], fromItem = (IList)from[i];
                        if (listItem.Count != fromItem.Count)
                        {
                            list[i] = from[i];
                        }
                        else
                        {
                            for (int k = 0; k < listItem.Count; k++)
                            {
                                if (!Equals(listItem[k], fromItem[k]))
                                {
                                    list[i] = from[i];
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Math.Min(list.Count, from.Count); i++)
                {
                    if (!Equals(list[i], from[i]))
                    {
                        list[i] = from[i];
                    }
                }
            }

            // Add missing items
            for (int i = list.Count; i < from.Count; i++)
            {
                list.Add(from[i]);
            }
        }