ReadOnlyDictionary.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace wf
  5. {
  6. public class ReadOnlyDictionary<Tkey, TVal> : IEnumerable<KeyValuePair<Tkey, TVal>>, IEnumerable
  7. {
  8. public ReadOnlyDictionary(Dictionary<Tkey, TVal> srcData)
  9. {
  10. this.srcData = srcData;
  11. }
  12. public TVal this[Tkey key]
  13. {
  14. get
  15. {
  16. return this.Get(key);
  17. }
  18. }
  19. public int Count
  20. {
  21. get
  22. {
  23. return this.srcData.Count;
  24. }
  25. }
  26. public bool ContainsKey(Tkey key)
  27. {
  28. return this.srcData.ContainsKey(key);
  29. }
  30. public TVal Get(Tkey key)
  31. {
  32. return this.srcData[key];
  33. }
  34. public Tkey[] GetKeyArray()
  35. {
  36. Tkey[] array = new Tkey[this.srcData.Count];
  37. int num = 0;
  38. foreach (KeyValuePair<Tkey, TVal> keyValuePair in this.srcData)
  39. {
  40. array[num++] = keyValuePair.Key;
  41. }
  42. return array;
  43. }
  44. public TVal[] GetValueArray()
  45. {
  46. TVal[] array = new TVal[this.srcData.Count];
  47. int num = 0;
  48. foreach (KeyValuePair<Tkey, TVal> keyValuePair in this.srcData)
  49. {
  50. array[num++] = keyValuePair.Value;
  51. }
  52. return array;
  53. }
  54. public Dictionary<Tkey, TVal> Copy()
  55. {
  56. return new Dictionary<Tkey, TVal>(this.srcData);
  57. }
  58. public IEnumerator<KeyValuePair<Tkey, TVal>> GetEnumerator()
  59. {
  60. return this.srcData.GetEnumerator();
  61. }
  62. IEnumerator IEnumerable.GetEnumerator()
  63. {
  64. return this.srcData.GetEnumerator();
  65. }
  66. private Dictionary<Tkey, TVal> srcData;
  67. }
  68. }