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.

100 lines
3.0 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 T Value { get; set; }
  9. /// <summary></summary>
  10. public bool IsNull
  11. {
  12. get { return Value == null; }
  13. }
  14. /// <summary></summary>
  15. public bool HasValue
  16. {
  17. get { return Value != null; }
  18. }
  19. /// <summary>创建默认值。</summary>
  20. public Class(T value = default, bool tryValueEquals = true, bool tryValueHashCode = true)
  21. {
  22. Value = value;
  23. _hashcode = tryValueHashCode;
  24. _equals = tryValueEquals;
  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. if (Value == null && obj == null) return true;
  41. if (Value == null && obj != null) return false;
  42. if (Value != null && obj == null) return false;
  43. return Value.Equals(obj);
  44. }
  45. return base.Equals(obj);
  46. }
  47. /// <summary></summary>
  48. public override string ToString()
  49. {
  50. if (Value == null) return null;
  51. return Value.ToString();
  52. }
  53. /// <summary></summary>
  54. /// <exception cref="MissingMemberException"></exception>
  55. /// <exception cref="NotSupportedException"></exception>
  56. public int CompareTo(object obj)
  57. {
  58. if (obj != null && obj is T) return CompareTo((T)obj);
  59. if (obj != null && obj is Class<T>) return CompareTo(obj as Class<T>);
  60. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  61. if (!(Value is IComparable)) throw new NotSupportedException();
  62. return ((IComparable)Value).CompareTo(obj);
  63. }
  64. /// <summary></summary>
  65. /// <exception cref="MissingMemberException"></exception>
  66. /// <exception cref="NotSupportedException"></exception>
  67. public int CompareTo(T other)
  68. {
  69. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  70. if (!(Value is IComparable)) throw new NotSupportedException();
  71. return ((IComparable<T>)Value).CompareTo(other);
  72. }
  73. /// <summary></summary>
  74. /// <exception cref="MissingMemberException"></exception>
  75. /// <exception cref="NotSupportedException"></exception>
  76. public int CompareTo(Class<T> other)
  77. {
  78. if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
  79. if (other == null || !other.HasValue) return 1;
  80. if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value);
  81. if (Value is IComparable<T>) return ((IComparable<T>)Value).CompareTo(other.Value);
  82. throw new NotSupportedException();
  83. }
  84. }