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.

148 lines
4.7 KiB

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