同时具备IList和IDictionary的特点的集合
1 [Serializable] 2 public class MyCollection:IList 3 { 4 private readonly Dictionary<string, MyItem> _dicMyItems; 5 private readonly List<MyItem> _myList; 6 //private readonly MyUIList _parent; 7 8 public MyCollection() : this(null) 9 { 10 11 } 12 13 public MyCollection(MyUIList parent) 14 { 15 //_parent = parent; 16 _myList = new List<MyItem>(); 17 _dicMyItems = new Dictionary<string, MyItem>(StringComparer.InvariantCultureIgnoreCase); 18 } 19 20 public MyItem this[string key] 21 { 22 get 23 { 24 MyItem item; 25 _dicMyItems.TryGetValue(key, out item); 26 if (item != null && item.Name == null) 27 { 28 return null; 29 } 30 return item; 31 } 32 } 33 34 35 public IEnumerator GetEnumerator() 36 { 37 return _myList.GetEnumerator(); 38 } 39 40 public void CopyTo(Array array, int index) 41 { 42 _myList.CopyTo((MyItem [])array, index); 43 } 44 45 public int Count 46 { 47 get { return _myList.Count; } 48 private set {} 49 } 50 51 public object SyncRoot { get; private set; } 52 public bool IsSynchronized { get; private set; } 53 public int Add(object value) 54 { 55 return AddHelper((MyItem)value); 56 } 57 58 public bool Contains(object value) 59 { 60 return _myList.Contains(Cast(value)); 61 } 62 63 public void Clear() 64 { 65 _myList.Clear(); 66 _dicMyItems.Clear(); 67 } 68 69 public int IndexOf(object value) 70 { 71 return _myList.IndexOf((MyItem)value); 72 } 73 74 public void Insert(int index, object value) 75 { 76 if (value == null) 77 { 78 return; 79 } 80 81 MyItem item = Cast(value); 82 Insert(index, item); 83 } 84 85 public void Remove(object value) 86 { 87 Remove(Cast(value)); 88 } 89 90 public void RemoveAt(int index) 91 { 92 if (index >= _myList.Count) 93 return; 94 Remove(_myList[index]); 95 } 96 97 public object this[int index] 98 { 99 get { return _myList[index]; } 100 set { _myList[index] = Cast(value); } 101 } 102 103 public bool IsReadOnly { get; private set; } 104 public bool IsFixedSize { get; private set; } 105 106 107 private int AddHelper(MyItem item) 108 { 109 if (item == null) 110 throw new ArgumentNullException("item"); 111 if (string.IsNullOrEmpty(item.Name) || _dicMyItems.ContainsKey(item.Name)) 112 { 113 throw new Exception("name is null or name already exists"); 114 } 115 //item.Parent = _parent; 116 int retVal = _myList.Count; 117 _myList.Add(item); 118 AddItemToDictionary(item); 119 return retVal; 120 } 121 122 private void AddItemToDictionary(MyItem item) 123 { 124 _dicMyItems.Add(item.Name, item); 125 } 126 127 private MyItem Cast(object value) 128 { 129 if (value == null) 130 { 131 throw new ArgumentNullException("value"); 132 } 133 134 var item = value as MyItem; 135 136 if (item == null) 137 throw new ArgumentException("DriverItem"); 138 139 return item; 140 } 141 142 public void Insert(int index, MyItem item) 143 { 144 if (item == null) 145 throw new ArgumentNullException("item"); 146 if (string.IsNullOrEmpty(item.Name) || _dicMyItems.ContainsKey(item.Name)) 147 { 148 throw new Exception("name is null or name already exists"); 149 } 150 //item.Parent = _parent; 151 _myList.Insert(index, item); 152 AddItemToDictionary(item); 153 } 154 155 public void Remove(MyItem item) 156 { 157 _myList.Remove(item); 158 _dicMyItems.Remove(item.Name); 159 } 160 }
其中 MyItem为自己定义的数据结构,可以换成自己的,或者改为通用型的T泛型
如果对你有帮助,请顶一下