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.

133 lines
4.4 KiB

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
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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 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
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. private bool _hashcode = false;
  8. private bool _equals = false;
  9. /// <summary>装箱对象。</summary>
  10. public T Value { get; set; }
  11. /// <summary>创建默认值。</summary>
  12. public Class(T value = default, bool tryEquals = true, bool tryHashCode = true)
  13. {
  14. Value = value;
  15. _hashcode = tryHashCode;
  16. _equals = tryEquals;
  17. }
  18. #region Override
  19. /// <summary></summary>
  20. public override int GetHashCode()
  21. {
  22. if (_hashcode && Value != null)
  23. {
  24. return Value.GetHashCode();
  25. }
  26. return base.GetHashCode();
  27. }
  28. /// <summary></summary>
  29. public override bool Equals(object obj)
  30. {
  31. if (_equals)
  32. {
  33. var right = obj as Class<T>;
  34. if (right == null) return false;
  35. if (Value == null && right.Value == null) return true;
  36. if (Value == null && right.Value != null) return false;
  37. if (Value != null && right.Value == null) return false;
  38. return Value.Equals(right.Value);
  39. }
  40. return base.Equals(obj);
  41. }
  42. /// <summary></summary>
  43. public override string ToString()
  44. {
  45. if (Value == null) return "";
  46. return Value.ToString();
  47. }
  48. #endregion
  49. #region IComparable
  50. /// <summary></summary>
  51. /// <exception cref="MissingMemberException"></exception>
  52. /// <exception cref="NotSupportedException"></exception>
  53. public int CompareTo(object obj)
  54. {
  55. if (obj != null && obj is T) return CompareTo((T)obj);
  56. if (obj != null && obj is Class<T>) return CompareTo(obj as Class<T>);
  57. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  58. if (!(Value is IComparable)) throw new NotSupportedException();
  59. return ((IComparable)Value).CompareTo(obj);
  60. }
  61. /// <summary></summary>
  62. /// <exception cref="MissingMemberException"></exception>
  63. /// <exception cref="NotSupportedException"></exception>
  64. public int CompareTo(T other)
  65. {
  66. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  67. if (!(Value is IComparable)) throw new NotSupportedException();
  68. return ((IComparable<T>)Value).CompareTo(other);
  69. }
  70. /// <summary></summary>
  71. /// <exception cref="MissingMemberException"></exception>
  72. /// <exception cref="NotSupportedException"></exception>
  73. public int CompareTo(Class<T> other)
  74. {
  75. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  76. if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value);
  77. if (Value is IComparable<T>) return ((IComparable<T>)Value).CompareTo(other.Value);
  78. throw new NotSupportedException();
  79. }
  80. #endregion
  81. #region 运算符。
  82. /// <summary>从 <see cref="Class{T}"/> 到 Boolean 的隐式转换,判断 <see cref="Class{T}"/> 包含值。</summary>
  83. /// <remarks>当 T 是 Boolean 时,获取 Value。<br />当 T 是 String 时,判断 Value 不为 NULL 且不为空。</remarks>
  84. public static implicit operator bool(Class<T> instance)
  85. {
  86. if (instance == null) return false;
  87. var boolean = instance as Class<bool>;
  88. if (boolean != null) return boolean.Value;
  89. var text = instance as Class<string>;
  90. if (text != null) return !string.IsNullOrEmpty(text.Value);
  91. return instance.NotNull();
  92. }
  93. /// <summary>从 <see cref="Class{T}"/> 到 T 的隐式转换。</summary>
  94. public static implicit operator T(Class<T> instance)
  95. {
  96. if (instance == null) return default(T);
  97. if (typeof(T).Equals(typeof(bool))) return instance.Value;
  98. return instance.Value;
  99. }
  100. /// <summary>从 T 到 <see cref="Class{T}"/> 的隐式转换。</summary>
  101. public static implicit operator Class<T>(T value) => new Class<T>(value);
  102. #endregion
  103. }
  104. }