KVComponent.cs 718 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections.Generic;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace Hotfix
  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. object k;
  23. if (!this.kv.TryGetValue(key, out k))
  24. {
  25. return default(T);
  26. }
  27. return (T) k;
  28. }
  29. public override void Dispose()
  30. {
  31. if (this.Id == 0)
  32. {
  33. return;
  34. }
  35. base.Dispose();
  36. }
  37. }
  38. }