BTEnv.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace ETModel
  5. {
  6. public class BTEnv: IEnumerable
  7. {
  8. public Dictionary<string, object> Values
  9. {
  10. get
  11. {
  12. return values;
  13. }
  14. }
  15. private Dictionary<string, object> values = new Dictionary<string, object>();
  16. public virtual void BeginInit()
  17. {
  18. if (this.values == null)
  19. {
  20. this.values = new Dictionary<string, object>();
  21. }
  22. }
  23. public virtual void EndInit()
  24. {
  25. if (this.values.Count == 0)
  26. {
  27. this.values = null;
  28. }
  29. }
  30. public object this[string key]
  31. {
  32. get
  33. {
  34. return this.values[key];
  35. }
  36. set
  37. {
  38. if (this.values == null)
  39. {
  40. this.values = new Dictionary<string, object>();
  41. }
  42. this.values[key] = value;
  43. }
  44. }
  45. public T Get<T>(string key)
  46. {
  47. if (this.values == null || !this.values.ContainsKey(key))
  48. {
  49. return default(T);
  50. }
  51. object value = values[key];
  52. try
  53. {
  54. return (T) value;
  55. }
  56. catch (InvalidCastException e)
  57. {
  58. throw new Exception($"不能把{value.GetType()}转换为{typeof (T)}", e);
  59. }
  60. }
  61. public void Set(string key, object obj)
  62. {
  63. if (this.values == null)
  64. {
  65. this.values = new Dictionary<string, object>();
  66. }
  67. this.values[key] = obj;
  68. }
  69. public bool ContainKey(string key)
  70. {
  71. if (this.values == null)
  72. {
  73. return false;
  74. }
  75. return this.values.ContainsKey(key);
  76. }
  77. public void Remove(string key)
  78. {
  79. if (this.values == null)
  80. {
  81. return;
  82. }
  83. this.values.Remove(key);
  84. if (this.values.Count == 0)
  85. {
  86. this.values = null;
  87. }
  88. }
  89. public void Add(string key, object value)
  90. {
  91. if (this.values == null)
  92. {
  93. this.values = new Dictionary<string, object>();
  94. }
  95. this.values[key] = value;
  96. }
  97. public IEnumerator GetEnumerator()
  98. {
  99. return this.values.GetEnumerator();
  100. }
  101. }
  102. }