using System;
/// 装箱类。
public class Class : IComparable, IComparable, IComparable>
{
private bool _hashcode = false;
private bool _equals = false;
/// 装箱对象。
public T Value { get; set; }
///
public bool IsNull
{
get { return Value == null; }
}
///
public bool HasValue
{
get { return Value != null; }
}
/// 创建默认值。
public Class(T value = default, bool tryValueEquals = true, bool tryValueHashCode = true)
{
Value = value;
_hashcode = tryValueHashCode;
_equals = tryValueEquals;
}
///
public override int GetHashCode()
{
if (_hashcode && Value != null)
{
return Value.GetHashCode();
}
return base.GetHashCode();
}
///
public override bool Equals(object obj)
{
if (_equals)
{
if (Value == null && obj == null) return true;
if (Value == null && obj != null) return false;
if (Value != null && obj == null) return false;
return Value.Equals(obj);
}
return base.Equals(obj);
}
///
public override string ToString()
{
if (Value == null) return null;
return Value.ToString();
}
///
///
///
public int CompareTo(object obj)
{
if (obj != null && obj is T) return CompareTo((T)obj);
if (obj != null && obj is Class) return CompareTo(obj as Class);
if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
if (!(Value is IComparable)) throw new NotSupportedException();
return ((IComparable)Value).CompareTo(obj);
}
///
///
///
public int CompareTo(T other)
{
if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
if (!(Value is IComparable)) throw new NotSupportedException();
return ((IComparable)Value).CompareTo(other);
}
///
///
///
public int CompareTo(Class other)
{
if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
if (other == null || !other.HasValue) return 1;
if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value);
if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value);
throw new NotSupportedException();
}
}