FieldValueCollection.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. namespace Ant.ORM
  7. {
  8. /// <summary>
  9. /// 缓存值类
  10. /// </summary>
  11. public class FieldValueCollection
  12. {
  13. private Dictionary<string, object> fields = new Dictionary<string, object>();
  14. private static object lockHelper = new object();
  15. private static volatile FieldValueCollection instance = null;
  16. public FieldValueCollection()
  17. {
  18. }
  19. public static FieldValueCollection Instance
  20. {
  21. get
  22. {
  23. if (instance == null)
  24. {
  25. lock (lockHelper)
  26. {
  27. if (instance == null)
  28. {
  29. instance = new FieldValueCollection();
  30. }
  31. }
  32. }
  33. return instance;
  34. }
  35. }
  36. #region IDictionary 成员
  37. /// <summary>
  38. /// 向集合中添加元素
  39. /// </summary>
  40. /// <param name="key"></param>
  41. /// <param name="value"></param>
  42. public void Add(string key, object value)
  43. {
  44. if (!Contains(key))
  45. fields.Add(key, value);
  46. else
  47. fields[key] = value;
  48. }
  49. /// <summary>
  50. /// 移除集合中所有元素
  51. /// </summary>
  52. public void Clear()
  53. {
  54. fields.Clear();
  55. }
  56. /// <summary>
  57. /// 判断集合中是否有该元素
  58. /// </summary>
  59. /// <param name="key"></param>
  60. /// <returns>有返true;没有返回false</returns>
  61. public bool Contains(string key)
  62. {
  63. return fields.ContainsKey(key);
  64. }
  65. public IDictionaryEnumerator GetEnumerator()
  66. {
  67. return fields.GetEnumerator();
  68. }
  69. public bool IsFixedSize
  70. {
  71. get { return false; }
  72. }
  73. public bool IsReadOnly
  74. {
  75. get { return false; }
  76. }
  77. public ICollection Keys
  78. {
  79. get { return fields.Keys; }
  80. }
  81. /// <summary>
  82. /// 移除集合中元素
  83. /// </summary>
  84. /// <param name="key"></param>
  85. public void Remove(string key)
  86. {
  87. fields.Remove(key);
  88. }
  89. /// <summary>
  90. /// 获取集合中值
  91. /// </summary>
  92. public ICollection Values
  93. {
  94. get { return fields.Values; }
  95. }
  96. /// <summary>
  97. /// 获取key的值
  98. /// </summary>
  99. /// <param name="key"></param>
  100. /// <returns></returns>
  101. public object this[string key]
  102. {
  103. get
  104. {
  105. if (Contains(key))
  106. return fields[key];
  107. else
  108. return null;
  109. }
  110. set
  111. {
  112. if (Contains(key))
  113. fields[key] = value;
  114. else
  115. fields.Add(key, value);
  116. }
  117. }
  118. #endregion
  119. #region ICollection 成员
  120. /// <summary>
  121. /// 将Dictionary复到Array中
  122. /// </summary>
  123. /// <param name="array"></param>
  124. /// <param name="index"></param>
  125. public void CopyTo(Array array, int index)
  126. {
  127. foreach (string key in fields.Keys)
  128. {
  129. FieldValue fv = new FieldValue(key, fields[key]);
  130. array.SetValue(fv, index);
  131. index++;
  132. }
  133. }
  134. /// <summary>
  135. /// 集合中所有元素的记录
  136. /// </summary>
  137. public int Count
  138. {
  139. get { return fields.Count; }
  140. }
  141. public bool IsSynchronized
  142. {
  143. get { return false; ; }
  144. }
  145. public object SyncRoot
  146. {
  147. get { return this; }
  148. }
  149. #endregion
  150. }
  151. }