StatusInfoCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. using System.Collections;
  7. namespace SuperSocket.SocketBase
  8. {
  9. /// <summary>
  10. /// Status information collection
  11. /// </summary>
  12. [Serializable]
  13. public class StatusInfoCollection
  14. {
  15. [NonSerialized]
  16. private Dictionary<string, object> m_Values = new Dictionary<string, object>();
  17. /// <summary>
  18. /// Gets the values.
  19. /// </summary>
  20. /// <value>
  21. /// The values.
  22. /// </value>
  23. public Dictionary<string, object> Values
  24. {
  25. get { return m_Values; }
  26. }
  27. /// <summary>
  28. /// Gets or sets the name.
  29. /// </summary>
  30. /// <value>
  31. /// The name.
  32. /// </value>
  33. public string Name { get; set; }
  34. /// <summary>
  35. /// Gets or sets the tag.
  36. /// </summary>
  37. /// <value>
  38. /// The tag.
  39. /// </value>
  40. public string Tag { get; set; }
  41. /// <summary>
  42. /// Gets or sets the collected time.
  43. /// </summary>
  44. /// <value>
  45. /// The collected time.
  46. /// </value>
  47. public DateTime CollectedTime { get; set; }
  48. /// <summary>
  49. /// Gets or sets the <see cref="System.Object" /> with the specified name.
  50. /// </summary>
  51. /// <value>
  52. /// The <see cref="System.Object" />.
  53. /// </value>
  54. /// <param name="name">The name.</param>
  55. /// <returns></returns>
  56. public object this[string name]
  57. {
  58. get
  59. {
  60. object value;
  61. if (m_Values.TryGetValue(name, out value))
  62. return value;
  63. return null;
  64. }
  65. set
  66. {
  67. m_Values[name] = value;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the value.
  72. /// </summary>
  73. /// <typeparam name="T"></typeparam>
  74. /// <param name="name">The name.</param>
  75. /// <param name="defaultValue">The default value.</param>
  76. /// <returns></returns>
  77. public T GetValue<T>(string name, T defaultValue)
  78. where T : struct
  79. {
  80. object value;
  81. if (m_Values.TryGetValue(name, out value))
  82. return (T)value;
  83. return defaultValue;
  84. }
  85. private List<KeyValuePair<string, object>> m_InternalList;
  86. [OnSerializing]
  87. private void OnSerializing(StreamingContext context)
  88. {
  89. m_InternalList = new List<KeyValuePair<string, object>>(m_Values.Count);
  90. foreach (var entry in m_Values)
  91. {
  92. m_InternalList.Add(new KeyValuePair<string, object>(entry.Key, entry.Value));
  93. }
  94. }
  95. [OnDeserialized]
  96. private void OnDeserialized(StreamingContext context)
  97. {
  98. if (m_InternalList == null || m_InternalList.Count <= 0)
  99. return;
  100. if (m_Values == null)
  101. m_Values = new Dictionary<string, object>();
  102. foreach (var entry in m_InternalList)
  103. {
  104. m_Values.Add(entry.Key, entry.Value);
  105. }
  106. m_InternalList = null;
  107. }
  108. }
  109. }