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.

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