using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace Update.Core.Entities { /// /// 增量更新包集合 /// public class DiffPackageCollection : KeyedCollection { /// /// 构造函数 /// public DiffPackageCollection() { } /// /// 构造函数 /// /// 元素集合 public DiffPackageCollection(IEnumerable items) { this.AddRange(items); } /// /// 获取主键 /// /// 元素 /// 主键 protected override Version GetKeyForItem(DiffPackage item) { return item.From; } /// /// 插入元素 /// /// 索引 /// 元素 protected override void InsertItem(int index, DiffPackage item) { try { base.InsertItem(index, item); } catch { throw new Exception(string.Format("增量更新包冲突 {0}。", item.FileName)); } } /// /// 添加集合 /// /// 元素集合 public void AddRange(IEnumerable range) { foreach (DiffPackage item in range) base.Add(item); } /// /// 获取指定主键元素 /// /// 主键 /// 获取到的元素 /// 包含返回true,否则返回false public bool TryGetValue(Version key, out DiffPackage item) { var dictionary = this.Dictionary; if (dictionary == null) { var comparer = this.Comparer; foreach (var value in this.Items) { if (comparer.Equals(this.GetKeyForItem(value), key)) { item = value; return true; } } item = null; return false; } return dictionary.TryGetValue(key, out item); } } }