C#、ASP.NET汎用ツールクラス?(数字、身分証明書、データ型などを判断できる)

7506 ワード

良いものはすべて人が整理して、分類する必要があります
注:ネームスペースSyntacticSugarを参照する必要があります
使用方法:

/***      ***/
 
//【IsInRange】 
int num = 100;
//    
if (num > 100 & num < 1000) { }
//    
if (num.IsInRange(100, 1000)) { } //datetime     
 
 
 
//【IsNullOrEmpty】
object s = "";
//    
if (s == null || string.IsNullOrEmpty(s.ToString())) { }
//    
if (s.IsNullOrEmpty()) { }
//       }
 
 
//【IsIn】
string value = "a";
//             
if (value == "a" || value == "b" || value == "c") {
}
//    
if (value.IsIn("a", "b", "c")) {
 
}
 
//【IsValuable IsNullOrEmpty  】
string ss = "";
//    
if (!string.IsNullOrEmpty(ss)) { }
//    
if (s.IsValuable()) { }
 
 
List list = null;
//    
if (list != null && list.Count > 0) { }
//    
if (list.IsValuable()) { }
 
 
 
 
//IsIDcard
if ("32061119810104311x".IsIDcard())
{
 
}
 
//IsTelephone
if ("0513-85669884".IsTelephone())
{
 
}
 
//IsMatch      Regex      
if ("    12".IsMatch(@" \d{2}")) { }
 
 
//                
//IsZero
//IsInt
//IsNoInt
//IsMoney
//IsEamil
//IsMobile


ソース:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace SyntacticSugar
{
  /// 
  /// **   :       ?
  /// **     :2015-5-29
  /// **     :-
  /// **   :sunkaixuan
  /// 
  public static class IsWhat
  {
    /// 
    ///      ?
    /// 
    /// 
    ///     begin
    ///     end
    /// 
    public static bool IsInRange(this int o, int begin, int end)
    {
      return o >= begin && o <= end;
    }
    /// 
    ///      ?
    /// 
    /// 
    ///     begin
    ///     end
    /// 
    public static bool IsInRange(this DateTime o, DateTime begin, DateTime end)
    {
      return o >= begin && o <= end;
    }
 
    /// 
    ///     ?
    /// 
    /// 
    /// 
    /// 
    /// 
    public static bool IsIn(this T o, params T[] values)
    {
      return values.Contains(o);
    }
 
    /// 
    ///  null ""?
    /// 
    /// 
    public static bool IsNullOrEmpty(this object o)
    {
      if (o == null || o == DBNull.Value) return true;
      return o.ToString() == "";
    }
    /// 
    ///  null ""?
    /// 
    /// 
    public static bool IsNullOrEmpty(this Guid? o)
    {
      if (o == null) return true;
      return o == Guid.Empty;
    }
    /// 
    ///  null ""?
    /// 
    /// 
    public static bool IsNullOrEmpty(this Guid o)
    {
      if (o == null) return true;
      return o == Guid.Empty;
    }
 
    /// 
    ///   ?( IsNullOrEmpty  )
    /// 
    /// 
    public static bool IsValuable(this object o)
    {
      if (o == null) return false;
      return o.ToString() != "";
    }
    /// 
    ///   ?( IsNullOrEmpty  )
    /// 
    /// 
    public static bool IsValuable(this IEnumerable o)
    {
      if (o == null || o.Count() == 0) return false;
      return true;
    }
 
    /// 
    ///   ?
    /// 
    /// 
    /// 
    public static bool IsZero(this object o)
    {
      return (o == null || o.ToString() == "0");
    }
 
    /// 
    ///  INT?
    /// 
    /// 
    /// 
    public static bool IsInt(this object o)
    {
      if (o == null) return false;
      return Regex.IsMatch(o.ToString(), @"^\d+$");
    }
    /// 
    ///   INT?
    /// 
    /// 
    /// 
    public static bool IsNoInt(this object o)
    {
      if (o == null) return true;
      return !Regex.IsMatch(o.ToString(), @"^\d+$");
    }
 
    /// 
    ///    ?
    /// 
    /// 
    /// 
    public static bool IsMoney(this object o)
    {
      if (o == null) return false;
      double outValue = 0;
      return double.TryParse(o.ToString(), out outValue);
    }
 
    /// 
    ///    ?
    /// 
    /// 
    /// 
    public static bool IsEamil(this object o)
    {
      if (o == null) return false;
      return Regex.IsMatch(o.ToString(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
    }
 
    /// 
    ///    ?
    /// 
    /// 
    /// 
    public static bool IsMobile(this object o)
    {
      if (o == null) return false;
      return Regex.IsMatch(o.ToString(), @"^\d{11}$");
    }
 
    /// 
    ///    ?
    /// 
    public static bool IsTelephone(this object o)
    {
      if (o == null) return false;
      return System.Text.RegularExpressions.Regex.IsMatch(o.ToString(), @"^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$");
 
    }
 
    /// 
    ///     ?
    /// 
    /// 
    /// 
    public static bool IsIDcard(this object o)
    {
      if (o == null) return false;
      return System.Text.RegularExpressions.Regex.IsMatch(o.ToString(), @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
    }
 
    /// 
    ///       ?
    /// 
    /// 
    ///     begin
    ///     end
    /// 
    public static bool IsMatch(this object o, string pattern)
    {
      if (o == null) return false;
      Regex reg = new Regex(pattern);
      return reg.IsMatch(o.ToString());
    }
  }
}