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.

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