using System; namespace Bruce.Lib { public class BUtil { /// <summary> /// 測試參數中是否含有null或空值 /// </summary> /// <param name="data">任意物件</param> /// <returns>含有null、DBNull或空值則為true,反之為false</returns> public static bool ContainsNullOrEmpty(params object[] data) { foreach (object d in data) { if (d == null || d == DBNull.Value || string.IsNullOrEmpty(d.ToString())) return true; } return false; } /// <summary> /// 將任意物件轉成字串,免去再判斷null或DBNull的處理 /// </summary> /// <param name="data">任意物件</param> /// <returns></returns> public static string StringValue(object data) { return data == null || data == DBNull.Value ? string.Empty : Convert.ToString(data); } /// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="length"></param> /// <returns></returns> public static string Summary(object data, int length) { if (length < 1) throw new ArgumentException(); string str = StringValue(data); return str.Length > length ? str.Substring(0, length) + "..." : str; } } }