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.

150 lines
4.8 KiB

4 years ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
4 years ago
11 months ago
4 years ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
4 years ago
11 months ago
4 years ago
4 years ago
11 months ago
4 years ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
11 months ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
11 months ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. namespace Apewer
  3. {
  4. /// <summary>装箱类。</summary>
  5. public sealed class Class<T> // : IComparable, IComparable<T>, IComparable<Class<T>>
  6. {
  7. /// <summary>值。</summary>
  8. public T Value { get; set; }
  9. /// <summary>创建装箱实例,值为默认值。</summary>
  10. public Class() { }
  11. /// <summary>创建装箱实例,值为指定值。</summary>
  12. public Class(T value) => Value = value;
  13. #region Override
  14. /// <summary></summary>
  15. public override int GetHashCode()
  16. {
  17. if (Value != null) return Value.GetHashCode();
  18. return base.GetHashCode();
  19. }
  20. /// <summary></summary>
  21. public override bool Equals(object obj)
  22. {
  23. if (ReferenceEquals(this, obj)) return true;
  24. var right = obj as Class<T>;
  25. if (Value.IsNull())
  26. {
  27. if (right == null) return true;
  28. if (right.Value.IsNull()) return true;
  29. return false;
  30. }
  31. else
  32. {
  33. if (right == null) return false;
  34. if (right.Value.IsNull()) return false;
  35. if (ReferenceEquals(Value, right.Value)) return true;
  36. return Value.Equals(right.Value);
  37. }
  38. }
  39. /// <summary></summary>
  40. public override string ToString()
  41. {
  42. if (Value == null) return null;
  43. return Value.ToString();
  44. }
  45. #endregion
  46. #if ClassCompare
  47. #region Compare
  48. /// <summary></summary>
  49. /// <exception cref="NotSupportedException"></exception>
  50. public int CompareTo(object obj)
  51. {
  52. if (typeof(IComparable).IsAssignableFrom(typeof(T)))
  53. {
  54. var comparable = Value as IComparable;
  55. if (comparable.IsNull())
  56. {
  57. if (obj is Class<T> bro)
  58. {
  59. if (bro == null) return 0;
  60. if (bro.Value.IsNull()) return 0;
  61. return -1;
  62. }
  63. if (obj.IsNull()) return 0;
  64. return -1;
  65. }
  66. else
  67. {
  68. if (obj != null && obj is Class<T> bro) return comparable.CompareTo(bro.Value);
  69. return comparable.CompareTo(obj);
  70. }
  71. }
  72. throw new NotImplementedException($"类型 {typeof(T).Name} 没有实现 {nameof(IComparable)} 接口。");
  73. }
  74. /// <summary></summary>
  75. /// <exception cref="MissingMemberException"></exception>
  76. /// <exception cref="NotSupportedException"></exception>
  77. public int CompareTo(T other)
  78. {
  79. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  80. if (!(Value is IComparable)) throw new NotSupportedException();
  81. return ((IComparable<T>)Value).CompareTo(other);
  82. }
  83. /// <summary></summary>
  84. /// <exception cref="MissingMemberException"></exception>
  85. /// <exception cref="NotSupportedException"></exception>
  86. public int CompareTo(Class<T> other)
  87. {
  88. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  89. if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value);
  90. if (Value is IComparable<T>) return ((IComparable<T>)Value).CompareTo(other.Value);
  91. throw new NotSupportedException();
  92. }
  93. #endregion
  94. #endif
  95. #region 运算符。
  96. /// <summary>从 <see cref="Class{T}"/> 到 Boolean 的隐式转换,判断 <see cref="Class{T}"/> 包含值。</summary>
  97. /// <remarks>当 T 是 Boolean 时,获取 Value。<br />当 T 是 String 时,判断 Value 不为 NULL 且不为空。</remarks>
  98. public static implicit operator bool(Class<T> instance)
  99. {
  100. if (instance == null) return false;
  101. var boolean = instance as Class<bool>;
  102. if (boolean != null) return boolean.Value;
  103. var text = instance as Class<string>;
  104. if (text != null) return !string.IsNullOrEmpty(text.Value);
  105. return instance != null;
  106. }
  107. /// <summary>从 <see cref="Class{T}"/> 到 T 的隐式转换。</summary>
  108. public static implicit operator T(Class<T> instance)
  109. {
  110. if (instance == null) return default(T);
  111. if (typeof(T).Equals(typeof(bool))) return instance.Value;
  112. return instance.Value;
  113. }
  114. /// <summary>从 T 到 <see cref="Class{T}"/> 的隐式转换。</summary>
  115. public static implicit operator Class<T>(T value) => new Class<T>(value);
  116. #endregion
  117. }
  118. }