Object.cs 802 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Generic;
  2. using MongoDB.Bson;
  3. namespace World
  4. {
  5. public class Object
  6. {
  7. public ObjectId Id { get; set; }
  8. public Dictionary<string, object> Dict { get; private set; }
  9. protected Object()
  10. {
  11. this.Id = ObjectId.GenerateNewId();
  12. this.Dict = new Dictionary<string, object>();
  13. }
  14. public object this[string key]
  15. {
  16. set
  17. {
  18. this.Dict[key] = value;
  19. }
  20. get
  21. {
  22. return this.Dict[key];
  23. }
  24. }
  25. public T Get<T>(string key)
  26. {
  27. if (!this.Dict.ContainsKey(key))
  28. {
  29. throw new KeyNotFoundException(string.Format("not found key: {0}", key));
  30. }
  31. return (T)this.Dict[key];
  32. }
  33. public bool Contain(string key)
  34. {
  35. return this.Dict.ContainsKey(key);
  36. }
  37. public bool Remove(string key)
  38. {
  39. return this.Dict.Remove(key);
  40. }
  41. }
  42. }