You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.7 KiB
133 lines
3.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
|
|
namespace Shentun.Utilities
|
|
{
|
|
public class ArgumentValidation
|
|
{
|
|
#region 判断对象是否为空
|
|
/// <summary>
|
|
/// 判断对象是否为空,为空返回true
|
|
/// </summary>
|
|
/// <typeparam name="T">要验证的对象的类型</typeparam>
|
|
/// <param name="data">要验证的对象</param>
|
|
public static bool IsNull<T>(T data)
|
|
{
|
|
//如果为null
|
|
if (data == null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
//如果为""
|
|
if (data.GetType() == typeof(String))
|
|
{
|
|
if (string.IsNullOrWhiteSpace(data.ToString().Trim()))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
//如果为DBNull
|
|
if (data.GetType() == typeof(DBNull))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
//不为空
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断对象是否为空,为空返回true
|
|
/// </summary>
|
|
/// <param name="data">要验证的对象</param>
|
|
public static bool IsNull(object data)
|
|
{
|
|
//如果为null
|
|
if (data == null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
//如果为""
|
|
if (data.GetType() == typeof(String))
|
|
{
|
|
if (string.IsNullOrWhiteSpace(data.ToString().Trim()))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
//如果为DBNull
|
|
if (data.GetType() == typeof(DBNull))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
//不为空
|
|
return false;
|
|
}
|
|
public static void CheckNull<T>(T data, string variableName)
|
|
{
|
|
if (IsNull<T>(data))
|
|
{
|
|
throw new Exception(variableName + "不能为空");
|
|
}
|
|
if(data is DateTime)
|
|
{
|
|
DateTime? dateTime ;
|
|
dateTime = data as DateTime?;
|
|
if (dateTime <= new DateTime(1900,1,1))
|
|
{
|
|
throw new Exception(variableName + "不能为空");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool IsNumber(string value)
|
|
{
|
|
try
|
|
{
|
|
decimal data = 0.0m;
|
|
if(value.ToUpper().Contains("E"))
|
|
{
|
|
data = Convert.ToDecimal(Decimal.Parse(value.ToString(), System.Globalization.NumberStyles.Float));
|
|
}
|
|
else
|
|
{
|
|
data = Convert.ToDecimal(value);
|
|
}
|
|
return true;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
return false;
|
|
}
|
|
//return Regex.IsMatch(value, "[\\+-]?[0-9]*(\\.[0-9])?([eE][\\+-]?[0-9]+)?");
|
|
|
|
}
|
|
|
|
public static void CheckIdNo(string idNo)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(idNo))
|
|
{
|
|
throw new Exception("身份证号不能是空值");
|
|
}
|
|
if (idNo.Length != 18)
|
|
throw new Exception("身份证号长度必须为18位");
|
|
|
|
var birthDate = Convert.ToDateTime(idNo.Substring(6, 4) + "-" +
|
|
idNo.Substring(10, 2) + "-" +
|
|
idNo.Substring(12, 2));
|
|
if (birthDate < new DateTime(1880, 1, 1) || birthDate > DateTime.Now.Date)
|
|
{
|
|
throw new Exception("身份证号中的出生日期不正确");
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|