KVComponent.cs 714 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections.Generic;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace Model
  4. {
  5. /// <summary>
  6. /// Key Value组件用于保存一些数据
  7. /// </summary>
  8. public class KVComponent: Component
  9. {
  10. [BsonElement]
  11. private readonly Dictionary<string, object> kv = new Dictionary<string, object>();
  12. public void Add(string key, object value)
  13. {
  14. this.kv.Add(key, value);
  15. }
  16. public void Remove(string key)
  17. {
  18. this.kv.Remove(key);
  19. }
  20. public T Get<T>(string key)
  21. {
  22. if (!this.kv.TryGetValue(key, out object k))
  23. {
  24. return default(T);
  25. }
  26. return (T) k;
  27. }
  28. public override void Dispose()
  29. {
  30. if (this.IsDisposed)
  31. {
  32. return;
  33. }
  34. base.Dispose();
  35. }
  36. }
  37. }