You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. #if !NET20
  6. using System.Dynamic;
  7. #endif
  8. namespace Apewer
  9. {
  10. /// <summary>文本字典模型。</summary>
  11. [Serializable]
  12. #if NET20
  13. public partial class TextSet
  14. #else
  15. public partial class TextSet : DynamicObject
  16. #endif
  17. {
  18. const string Null = "";
  19. bool _autotrim = false;
  20. bool _locked = false;
  21. Dictionary<string, string> _origin;
  22. #region properties
  23. /// <summary>获取或设置字典内容。</summary>
  24. public string this[string key] { get { return Get(key); } set { Set(key, value); } }
  25. /// <summary>获取字典。</summary>
  26. internal Dictionary<string, string> Origin { get { return _origin; } }
  27. #endregion
  28. #region ctor
  29. internal bool Locked { get { return _locked; } set { _locked = value; } }
  30. private void Constructor(bool autotrim, Dictionary<string, string> origin)
  31. {
  32. _origin = new Dictionary<string, string>();
  33. _autotrim = autotrim;
  34. if (origin != null)
  35. {
  36. foreach (var kvp in origin)
  37. {
  38. var key = kvp.Key;
  39. if (_autotrim && key.Length > 0) key = key.Trim();
  40. if (_origin.ContainsKey(key)) continue;
  41. var value = kvp.Value;
  42. if (_autotrim && value.Length > 0) value = value.Trim();
  43. _origin.Add(key, kvp.Value ?? "");
  44. }
  45. }
  46. }
  47. #endregion
  48. #region methods
  49. /// <summary>构造函数。</summary>
  50. public TextSet(bool autotrim = false)
  51. {
  52. Constructor(autotrim, null);
  53. }
  54. /// <summary>构造函数:从指定字典中导入。</summary>
  55. public TextSet(Dictionary<string, string> origin, bool autotrim)
  56. {
  57. Constructor(autotrim, origin);
  58. }
  59. /// <summary>构造函数:从指定实例中导入。</summary>
  60. public TextSet(TextSet origin, bool autotrim)
  61. {
  62. Constructor(autotrim, (origin == null) ? null : origin.Origin);
  63. }
  64. /// <summary>构造函数:从指定实例中导入。</summary>
  65. public TextSet(ObjectSet<string> origin, bool autotrim)
  66. {
  67. Constructor(autotrim, (origin == null) ? null : origin.Origin);
  68. }
  69. private string Get(string key)
  70. {
  71. var contains = false;
  72. return Get(key, ref contains);
  73. }
  74. private string Get(string key, ref bool contains)
  75. {
  76. var k = string.IsNullOrEmpty(key) ? "" : ((_autotrim) ? key.Trim() : key);
  77. var value = Null;
  78. lock (_origin)
  79. {
  80. contains = _origin.ContainsKey(k);
  81. if (contains) value = _origin[k];
  82. }
  83. return value ?? Null;
  84. }
  85. private bool Set(string key, string value)
  86. {
  87. if (_locked) return false;
  88. var k = string.IsNullOrEmpty(key) ? "" : ((_autotrim) ? key.Trim() : key);
  89. var v = string.IsNullOrEmpty(value) ? "" : ((_autotrim) ? value.Trim() : value);
  90. lock (_origin)
  91. {
  92. if (_origin.ContainsKey(k))
  93. {
  94. _origin[k] = v ?? Null;
  95. }
  96. else
  97. {
  98. _origin.Add(k, v);
  99. }
  100. }
  101. return true;
  102. }
  103. #endregion
  104. #region operator
  105. /// <summary></summary>
  106. public static implicit operator TextSet(Dictionary<string, string> dictionary) => new TextSet(dictionary, false);
  107. /// <summary></summary>
  108. public static implicit operator Dictionary<string, string>(TextSet set) => set?._origin;
  109. #endregion
  110. #region dynamic
  111. #if !NET20
  112. /// <summary></summary>
  113. public override bool TryGetMember(GetMemberBinder binder, out object result)
  114. {
  115. var contains = false;
  116. result = Get(binder.Name, ref contains);
  117. return contains;
  118. }
  119. /// <summary></summary>
  120. public override bool TrySetMember(SetMemberBinder binder, object value)
  121. {
  122. return Set(binder.Name, value as string);
  123. }
  124. /// <summary></summary>
  125. public override IEnumerable<string> GetDynamicMemberNames()
  126. {
  127. return _origin.Keys;
  128. }
  129. #endif
  130. #endregion
  131. }
  132. }