Object.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections.Generic;
  2. using MongoDB.Bson;
  3. namespace Common.Component
  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. return default(T);
  30. }
  31. return (T) this.Dict[key];
  32. }
  33. public T Get<T>()
  34. {
  35. return this.Get<T>(typeof (T).Name);
  36. }
  37. public void Set(string key, object obj)
  38. {
  39. this.Dict[key] = obj;
  40. }
  41. public void Set<T>(T obj)
  42. {
  43. this.Dict[typeof (T).Name] = obj;
  44. }
  45. public bool Contain(string key)
  46. {
  47. return this.Dict.ContainsKey(key);
  48. }
  49. public bool Remove(string key)
  50. {
  51. return this.Dict.Remove(key);
  52. }
  53. }
  54. }