C#ASP.NET汎用拡張関数のIsWhatは簡単で使いやすい

9854 ワード

良いものはすべて人が整理して、分類する必要があります
 
 
注:ネームスペース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<string> 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
{
    /// <summary>
    /// **   :       ?
    /// **     :2015-5-29
    /// **     :-
    /// **   :sunkaixuan
    /// **     :http://www.cnblogs.com/sunkaixuan/p/4539654.html
    /// </summary>
    public static class IsWhat
    {
        /// <summary>
        ///      ?
        /// </summary>
        /// <param name="o"></param>
        /// <param name="begin">    begin</param>
        /// <param name="end">    end</param>
        /// <returns></returns>
        public static bool IsInRange(this int o, int begin, int end)
        {
            return o >= begin && o <= end;
        }
        /// <summary>
        ///      ?
        /// </summary>
        /// <param name="o"></param>
        /// <param name="begin">    begin</param>
        /// <param name="end">    end</param>
        /// <returns></returns>
        public static bool IsInRange(this DateTime o, DateTime begin, DateTime end)
        {
            return o >= begin && o <= end;
        }

        /// <summary>
        ///     ?
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static bool IsIn<T>(this T o, params T[] values)
        {
            return values.Contains(o);
        }

        /// <summary>
        ///  null ""?
        /// </summary>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this object o)
        {
            if (o == null || o == DBNull.Value) return true;
            return o.ToString() == "";
        }
        /// <summary>
        ///  null ""?
        /// </summary>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this Guid? o)
        {
            if (o == null) return true;
            return o == Guid.Empty;
        }
        /// <summary>
        ///  null ""?
        /// </summary>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this Guid o)
        {
            if (o == null) return true;
            return o == Guid.Empty;
        }

        /// <summary>
        ///   ?( IsNullOrEmpty  )
        /// </summary>
        /// <returns></returns>
        public static bool IsValuable(this object o)
        {
            if (o == null) return false;
            return o.ToString() != "";
        }
        /// <summary>
        ///   ?( IsNullOrEmpty  )
        /// </summary>
        /// <returns></returns>
        public static bool IsValuable(this IEnumerable<object> o)
        {
            if (o == null || o.Count() == 0) return false;
            return true;
        }

        /// <summary>
        ///   ?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsZero(this object o)
        {
            return (o == null || o.ToString() == "0");
        }

        /// <summary>
        ///  INT?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsInt(this object o)
        {
            if (o == null) return false;
            return Regex.IsMatch(o.ToString(), @"^\d+$");
        }
        /// <summary>
        ///   INT?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsNoInt(this object o)
        {
            if (o == null) return true;
            return !Regex.IsMatch(o.ToString(), @"^\d+$");
        }

        /// <summary>
        ///    ?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsMoney(this object o)
        {
            if (o == null) return false;
            double outValue = 0;
            return double.TryParse(o.ToString(), out outValue);
        }

        /// <summary>
        ///    ?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsEamil(this object o)
        {
            if (o == null) return false;
            return Regex.IsMatch(o.ToString(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
        }

        /// <summary>
        ///    ?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsMobile(this object o)
        {
            if (o == null) return false;
            return Regex.IsMatch(o.ToString(), @"^\d{11}$");
        }

        /// <summary>
        ///    ?
        /// </summary>
        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}$");

        }

        /// <summary>
        ///     ?
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        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))$");
        }

        /// <summary>
        ///       ?
        /// </summary>
        /// <param name="o"></param>
        /// <param name="begin">    begin</param>
        /// <param name="end">    end</param>
        /// <returns></returns>
        public static bool IsMatch(this object o, string pattern)
        {
            if (o == null) return false;
            Regex reg = new Regex(pattern);
            return reg.IsMatch(o.ToString());
        }
    }
}