当前位置: 代码迷 >> .NET相关 >> 稽查对象是否为NULL或者为Empty
  详细解决方案

稽查对象是否为NULL或者为Empty

热度:184   发布时间:2016-04-24 02:52:39.0
检查对象是否为NULL或者为Empty

不管是在Winform开发,还是在asp.net 开发中当从一个数据源中获取数据时你总是不知道这个数据的状态,这个时候总要对她进行一次判断,不过每次进行一次判断总是要写怎么一堆代码,
时间长了,总感觉不太好,于是就有了下面代码的封装,这是一个扩展的泛型方法,写在了System.命名空间下面,省去了重复写这么多代码的时间,每次只要原点调用以下就可以了.

呵呵,

 1 namespace System 2 { 3     public static class CheckValueExten 4     { 5         /// <summary> 6         /// 检查当前对象是否为NULL,或者string是NULLOrEmpty 7         /// </summary> 8         /// <typeparam name="T"></typeparam> 9         /// <param name="str"></param>10         /// <returns></returns>11         public static bool CheckValue<T>(this  T str)12         {13             if (str is string)14             {15                 if (!string.IsNullOrEmpty(str.ToString()))16                 {17                     return true;18                 }19                 else20                 {21                     return false;22                 }23             }24             else25             {26                 if (str == null)27                 {28                     return false;29                 }30                 else31                 {32                     return true;33                 }34             }35         }36     }37 }
源代码
1楼梦在旅途
可以优化成如下:,public static bool CheckValuelt;Tgt;(this T str) { if(str is string) { return (!string.IsNullOrEmpty(str));
  相关解决方案