Object.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Base
  7. {
  8. public abstract class Object: ISupportInitialize, IDisposable, IEnumerable
  9. {
  10. public static ObjectManager ObjectManager = new ObjectManager();
  11. [BsonId]
  12. public long Id { get; private set; }
  13. public Dictionary<string, object> Values
  14. {
  15. get
  16. {
  17. return values;
  18. }
  19. }
  20. [BsonElement, BsonIgnoreIfNull]
  21. private Dictionary<string, object> values = new Dictionary<string, object>();
  22. protected Object()
  23. {
  24. Id = IdGenerater.GenerateId();
  25. }
  26. protected Object(long id)
  27. {
  28. this.Id = id;
  29. }
  30. public virtual void BeginInit()
  31. {
  32. if (this.values == null)
  33. {
  34. this.values = new Dictionary<string, object>();
  35. }
  36. }
  37. public virtual void EndInit()
  38. {
  39. if (this.values.Count == 0)
  40. {
  41. this.values = null;
  42. }
  43. }
  44. public object this[string key]
  45. {
  46. get
  47. {
  48. return this.values[key];
  49. }
  50. set
  51. {
  52. if (this.values == null)
  53. {
  54. this.values = new Dictionary<string, object>();
  55. }
  56. this.values[key] = value;
  57. }
  58. }
  59. public T Get<T>(string key)
  60. {
  61. if (this.values == null || !this.values.ContainsKey(key))
  62. {
  63. return default(T);
  64. }
  65. object value = values[key];
  66. return (T)value;
  67. }
  68. public void Set(string key, object obj)
  69. {
  70. if (this.values == null)
  71. {
  72. this.values = new Dictionary<string, object>();
  73. }
  74. this.values[key] = obj;
  75. }
  76. public bool ContainKey(string key)
  77. {
  78. if (this.values == null)
  79. {
  80. return false;
  81. }
  82. return this.values.ContainsKey(key);
  83. }
  84. public void Remove(string key)
  85. {
  86. if (this.values == null)
  87. {
  88. return;
  89. }
  90. this.values.Remove(key);
  91. if (this.values.Count == 0)
  92. {
  93. this.values = null;
  94. }
  95. }
  96. public void Add(string key, object value)
  97. {
  98. if (this.values == null)
  99. {
  100. this.values = new Dictionary<string, object>();
  101. }
  102. this.values[key] = value;
  103. }
  104. public IEnumerator GetEnumerator()
  105. {
  106. return this.values.GetEnumerator();
  107. }
  108. public bool IsDisposed()
  109. {
  110. return this.Id == 0;
  111. }
  112. public virtual void Dispose()
  113. {
  114. if (this.Id == 0)
  115. {
  116. return;
  117. }
  118. this.Id = 0;
  119. }
  120. }
  121. }