初始化
static BComparer()
{
//基礎類型比較器
Type t = typeof(T);
if (t == typeof(bool))
{
Comparison<bool> cmp_bool = delegate(bool t1, bool t2)
{ return t1 == t2 ? 0 : (t1 ? 1 : -1); };
_map.Add(typeof(bool), (Delegate)cmp_bool);
}
if (t == typeof(int))
{
Comparison<int> cmp_int = delegate(int t1, int t2)
{ return t1 > t2 ? 1 : (t1 == t2 ? 0 : -1); };
_map.Add(typeof(int), (Delegate)cmp_int);
}
//....其他
}
//注冊自定義比較
public static void Register<NT>(Comparison<NT> comparison)
{
System.Diagnostics.Debug.Assert(_map.ContainsKey(typeof(NT)) == false);
_map.Add(typeof(NT), (Delegate)comparison);
}
//比較函數,以後用來實現IComparer用
public int Compare(T t1, T t2)
{
System.Diagnostics.Debug.Assert(_comparison != null);
return _comparison(t1, t2);
}