//查询算法:二分查找
/// <summary>
///查询算法:二分查找
/// </summary>
/// <param name="listObj">待查询的对象集合</param>
/// <param name="pkid">查询关键字</param>
/// <returns>返回关键字在集合中的位置</returns>
public static int binarySearch<TObject>(List<TObject> listObj, int pkid)
{
int pos = -1;
int low, high, mid;
int PKID;
if (listObj.Count > 1)
{
low = 0;
high = listObj.Count - 1;
mid = (low + high) / 2;
while (low <= high)
{
PKID = (int)typeof(TObject).GetProperty("pkid").GetValue(listObj[mid], null);
if (PKID == pkid)
{
pos = mid;
break;
}
else if (PKID > pkid)
{
high = mid - 1;
mid = (low + high) / 2;
}
else
{
low = mid + 1;
mid = (low + high) / 2;
}
}
}
else if (listObj.Count == 1)
{
PKID = (int)typeof(TObject).GetProperty("pkid").GetValue(listObj[0], null);
if (PKID == pkid)
{
pos = 0;
}
}
else
{
pos = -1;
}
return pos;
}