AggregateDictionary.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // System.Runtime.Remoting.Channels.AggregateDictionary.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2002 (C) Copyright, Novell, Inc.
  7. //
  8. using System.Collections;
  9. namespace System.Runtime.Remoting.Channels.Http
  10. {
  11. internal class AggregateDictionary: IDictionary
  12. {
  13. IDictionary[] dictionaries;
  14. ArrayList _values;
  15. ArrayList _keys;
  16. public AggregateDictionary (IDictionary[] dics)
  17. {
  18. dictionaries = dics;
  19. }
  20. public bool IsFixedSize
  21. {
  22. get { return true; }
  23. }
  24. public bool IsReadOnly
  25. {
  26. get { return true; }
  27. }
  28. public object this [object key]
  29. {
  30. get
  31. {
  32. foreach (IDictionary dic in dictionaries)
  33. if (dic.Contains (key)) return dic [key];
  34. return null;
  35. }
  36. set
  37. {
  38. throw new NotSupportedException ();
  39. }
  40. }
  41. public ICollection Keys
  42. {
  43. get
  44. {
  45. if (_keys != null) return _keys;
  46. _keys = new ArrayList ();
  47. foreach (IDictionary dic in dictionaries)
  48. _keys.AddRange (dic.Keys);
  49. return _keys;
  50. }
  51. }
  52. public ICollection Values
  53. {
  54. get
  55. {
  56. if (_values != null) return _values;
  57. _values = new ArrayList ();
  58. foreach (IDictionary dic in dictionaries)
  59. _values.AddRange (dic.Values);
  60. return _values;
  61. }
  62. }
  63. public void Add (object key, object value)
  64. {
  65. throw new NotSupportedException ();
  66. }
  67. public void Clear ()
  68. {
  69. throw new NotSupportedException ();
  70. }
  71. public bool Contains (object ob)
  72. {
  73. foreach (IDictionary dic in dictionaries)
  74. if (dic.Contains (ob)) return true;
  75. return false;
  76. }
  77. public IDictionaryEnumerator GetEnumerator ()
  78. {
  79. return new AggregateEnumerator (dictionaries);
  80. }
  81. IEnumerator IEnumerable.GetEnumerator ()
  82. {
  83. return new AggregateEnumerator (dictionaries);
  84. }
  85. public void Remove (object ob)
  86. {
  87. throw new NotSupportedException ();
  88. }
  89. public void CopyTo (Array array, int index)
  90. {
  91. foreach (object ob in this)
  92. array.SetValue (ob, index++);
  93. }
  94. public int Count
  95. {
  96. get
  97. {
  98. int c = 0;
  99. foreach (IDictionary dic in dictionaries)
  100. c += dic.Count;
  101. return c;
  102. }
  103. }
  104. public bool IsSynchronized
  105. {
  106. get { return false; }
  107. }
  108. public object SyncRoot
  109. {
  110. get { return this; }
  111. }
  112. }
  113. internal class AggregateEnumerator: IDictionaryEnumerator
  114. {
  115. IDictionary[] dictionaries;
  116. int pos = 0;
  117. IDictionaryEnumerator currente;
  118. public AggregateEnumerator (IDictionary[] dics)
  119. {
  120. dictionaries = dics;
  121. Reset ();
  122. }
  123. public DictionaryEntry Entry
  124. {
  125. get { return currente.Entry; }
  126. }
  127. public object Key
  128. {
  129. get { return currente.Key; }
  130. }
  131. public object Value
  132. {
  133. get { return currente.Value; }
  134. }
  135. public object Current
  136. {
  137. get { return currente.Current; }
  138. }
  139. public bool MoveNext ()
  140. {
  141. if (pos >= dictionaries.Length) return false;
  142. if (!currente.MoveNext())
  143. {
  144. pos++;
  145. if (pos >= dictionaries.Length) return false;
  146. currente = dictionaries [pos].GetEnumerator ();
  147. return MoveNext ();
  148. }
  149. return true;
  150. }
  151. public void Reset ()
  152. {
  153. pos = 0;
  154. if (dictionaries.Length > 0)
  155. currente = dictionaries [0].GetEnumerator ();
  156. }
  157. }
  158. }