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.

103 lines
3.1 KiB

  1. using System;
  2. /// <summary>装箱类。</summary>
  3. public 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 virtual T Value { get; set; }
  9. /// <summary></summary>
  10. public virtual bool IsNull
  11. {
  12. get { return Value == null; }
  13. }
  14. /// <summary></summary>
  15. public virtual bool HasValue
  16. {
  17. get { return Value != null; }
  18. }
  19. /// <summary>创建默认值。</summary>
  20. public Class(T value = default, bool tryEquals = true, bool tryHashCode = true)
  21. {
  22. Value = value;
  23. _hashcode = tryHashCode;
  24. _equals = tryEquals;
  25. }
  26. /// <summary></summary>
  27. public override int GetHashCode()
  28. {
  29. if (_hashcode && Value != null)
  30. {
  31. return Value.GetHashCode();
  32. }
  33. return base.GetHashCode();
  34. }
  35. /// <summary></summary>
  36. public override bool Equals(object obj)
  37. {
  38. if (_equals)
  39. {
  40. var right = obj as Class<T>;
  41. if (right == null) return false;
  42. if (Value == null && right.Value == null) return true;
  43. if (Value == null && right.Value != null) return false;
  44. if (Value != null && right.Value == null) return false;
  45. return Value.Equals(right.Value);
  46. }
  47. return base.Equals(obj);
  48. }
  49. /// <summary></summary>
  50. public override string ToString()
  51. {
  52. if (Value == null) return "";
  53. return Value.ToString();
  54. }
  55. /// <summary></summary>
  56. /// <exception cref="MissingMemberException"></exception>
  57. /// <exception cref="NotSupportedException"></exception>
  58. public virtual int CompareTo(object obj)
  59. {
  60. if (obj != null && obj is T) return CompareTo((T)obj);
  61. if (obj != null && obj is Class<T>) return CompareTo(obj as Class<T>);
  62. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  63. if (!(Value is IComparable)) throw new NotSupportedException();
  64. return ((IComparable)Value).CompareTo(obj);
  65. }
  66. /// <summary></summary>
  67. /// <exception cref="MissingMemberException"></exception>
  68. /// <exception cref="NotSupportedException"></exception>
  69. public virtual int CompareTo(T other)
  70. {
  71. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  72. if (!(Value is IComparable)) throw new NotSupportedException();
  73. return ((IComparable<T>)Value).CompareTo(other);
  74. }
  75. /// <summary></summary>
  76. /// <exception cref="MissingMemberException"></exception>
  77. /// <exception cref="NotSupportedException"></exception>
  78. public virtual int CompareTo(Class<T> other)
  79. {
  80. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  81. if (other == null || !other.HasValue) return 1;
  82. if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value);
  83. if (Value is IComparable<T>) return ((IComparable<T>)Value).CompareTo(other.Value);
  84. throw new NotSupportedException();
  85. }
  86. }