示例#1
0
        /// <summary>
        /// 如果测试对象为空,则返回一个默认对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="testObject">测试对象</param>
        /// <param name="objectForNothing">对象为空时返回的默认对象</param>
        /// <param name="nothingType">空对象类型</param>
        /// <returns>如果对象为空,返回一个默认对象;否则,返回对象本身</returns>
        public static T IfNothing <T>(this T testObject, T objectForNothing, NothingType nothingType = NothingType.All)
        {
            if (IsNullOrEmpty(testObject, nothingType))
            {
                return(objectForNothing);
            }

            return(testObject);
        }
示例#2
0
        /// <summary>
        /// 对象是否为空
        /// </summary>
        /// <param name="testObject">待测试对象</param>
        /// <param name="nothingType">空对象类型</param>
        /// <returns>对象为空返回True,否则返回False</returns>
        public static bool IsNullOrEmpty(this object testObject, NothingType nothingType = NothingType.All)
        {
            // 对引用判断
            if ((int)(nothingType & NothingType.Reference) != 0)
            {
                if (testObject == null)
                {
                    return(true);
                }
            }

            // 对DBNull的判断
            if ((int)(nothingType & NothingType.DBNull) != 0)
            {
                if (testObject == Convert.DBNull)
                {
                    return(true);
                }
            }

            // 对List的判断
            if ((int)(nothingType & NothingType.List) != 0)
            {
                if (testObject is ICollection && (testObject as ICollection).Count == 0)
                {
                    return(true);
                }
            }

            // 对String的判断
            if ((int)(nothingType & NothingType.String) != 0)
            {
                if (testObject is string && (testObject as string) == String.Empty)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#3
0
 /// <summary>
 /// 对象是否为不空
 /// </summary>
 /// <param name="testObject">待测试对象</param>
 /// <param name="nothingType">空对象类型</param>
 /// <returns>对象为空返回True,否则返回False</returns>
 public static bool IsNotNullOrEmpty(this object testObject, NothingType nothingType = NothingType.All)
 {
     return(!IsNullOrEmpty(testObject, nothingType));
 }
 public object Read(NothingType nothingType) => null;
示例#5
0
 public void Write(NothingType nothingType, object value)
 {
 }