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.
|
|
using System;
namespace Apewer {
/// <summary>装箱类。</summary>
public sealed class Class<T> : IComparable, IComparable<T>, IComparable<Class<T>> {
private bool _hashcode = false; private bool _equals = false;
/// <summary>装箱对象。</summary>
public T Value { get; set; }
/// <summary>创建默认值。</summary>
public Class(T value = default, bool tryEquals = true, bool tryHashCode = true) { Value = value; _hashcode = tryHashCode; _equals = tryEquals; }
#region Override
/// <summary></summary>
public override int GetHashCode() { if (_hashcode && Value != null) { return Value.GetHashCode(); } return base.GetHashCode(); }
/// <summary></summary>
public override bool Equals(object obj) { if (_equals) { var right = obj as Class<T>; if (right == null) return false;
if (Value == null && right.Value == null) return true; if (Value == null && right.Value != null) return false; if (Value != null && right.Value == null) return false; return Value.Equals(right.Value); } return base.Equals(obj); }
/// <summary></summary>
public override string ToString() { if (Value == null) return ""; return Value.ToString(); }
#endregion
#region IComparable
/// <summary></summary>
/// <exception cref="MissingMemberException"></exception>
/// <exception cref="NotSupportedException"></exception>
public int CompareTo(object obj) { if (obj != null && obj is T) return CompareTo((T)obj); if (obj != null && obj is Class<T>) return CompareTo(obj as Class<T>);
if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value)); if (!(Value is IComparable)) throw new NotSupportedException(); return ((IComparable)Value).CompareTo(obj); }
/// <summary></summary>
/// <exception cref="MissingMemberException"></exception>
/// <exception cref="NotSupportedException"></exception>
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<T>)Value).CompareTo(other); }
/// <summary></summary>
/// <exception cref="MissingMemberException"></exception>
/// <exception cref="NotSupportedException"></exception>
public int CompareTo(Class<T> other) { if (Value == null) throw new MissingMemberException(typeof(T).FullName, nameof(Value));
if (Value is IComparable) return ((IComparable)Value).CompareTo(other.Value); if (Value is IComparable<T>) return ((IComparable<T>)Value).CompareTo(other.Value);
throw new NotSupportedException(); }
#endregion
#region 运算符。
/// <summary>从 <see cref="Class{T}"/> 到 Boolean 的隐式转换,判断 <see cref="Class{T}"/> 包含值。</summary>
/// <remarks>当 T 是 Boolean 时,获取 Value。<br />当 T 是 String 时,判断 Value 不为 NULL 且不为空。</remarks>
public static implicit operator bool(Class<T> instance) { if (instance == null) return false;
var boolean = instance as Class<bool>; if (boolean != null) return boolean.Value;
var text = instance as Class<string>; if (text != null) return !string.IsNullOrEmpty(text.Value);
return instance.NotNull(); }
/// <summary>从 <see cref="Class{T}"/> 到 T 的隐式转换。</summary>
public static implicit operator T(Class<T> instance) { if (instance == null) return default(T);
if (typeof(T).Equals(typeof(bool))) return instance.Value; return instance.Value; }
/// <summary>从 T 到 <see cref="Class{T}"/> 的隐式转换。</summary>
public static implicit operator Class<T>(T value) => new Class<T>(value);
#endregion
}
}
|