方法的定义:(方法的返回值和参数都是泛型)
public static T[] GetAllDataBySort<T>(string sort) where T : NormalObj
{
return GetAllDataBySort<T>(sort, "");
}
public static T[] GetAllDataBySort<T>(string sort, string special) where T : NormalObj
{
string sql;
if (!string.IsNullOrEmpty(special))
sql = string.Format("select * from {0} where {1} order by {2}", typeof(T).Name, special, sort);
else
sql = string.Format("select * from {0} order by {1}", typeof(T).Name, sort);
T[] obj = default(T[]);
obj = Sql.select<T>(sql);
if (obj != null)
return obj;
throw new Exception("异常");
}
/// <summary>
/// 根据对象所限定条件获得一定的对象集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T[] GetSomeObject<T>() where T : NormalObj, new()//new() 规定T必须带无参构造函数
{
return GetSomeObject<T>("");
}
方法的调用:
public static DB.TravelObj.City[] GetCitiesByCompanyId(long CompanyId)
{
City obj = new City();
obj.Cstate = 1;
obj.CcompanyId = CompanyId;
return obj.GetSomeObject<DB.TravelObj.City>();
}
联系客服