189 lines
6.0 KiB
C#
189 lines
6.0 KiB
C#
using System;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace Zhaizj.Framework.Utils
|
||
{
|
||
/// <summary>
|
||
/// 关于参数验证的一些实用方法
|
||
/// </summary>
|
||
public static class Checker
|
||
{
|
||
/// <summary>
|
||
/// 检查字符串是否为空(null或者长度为0)
|
||
/// </summary>
|
||
/// <param name="argName">字符串名</param>
|
||
/// <param name="argValue">被检查的字符串</param>
|
||
/// <param name="throwError">为空时是否抛出异常</param>
|
||
/// <returns>为空则返回true</returns>
|
||
public static bool CheckEmptyString(string argName, string argValue, bool throwError) {
|
||
CheckArgumentNull("argName", argName, true);
|
||
bool ret = (argValue == null || argValue.Length == 0);
|
||
if(ret && throwError) {
|
||
throw new ArgumentException("字符串为空", argName);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查参数是否为空引用(null)
|
||
/// </summary>
|
||
/// <param name="argName">参数名</param>
|
||
/// <param name="argValue">被检查的参数</param>
|
||
/// <param name="throwError">为空引用时是否抛出异常</param>
|
||
/// <returns>为空则返回true</returns>
|
||
public static bool CheckArgumentNull(string argName, object argValue, bool throwError) {
|
||
if(argName == null) {
|
||
throw new ArgumentNullException("argName");
|
||
}
|
||
if(argValue == null) {
|
||
if(throwError) {
|
||
throw new ArgumentNullException(argName);
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查数组是否为空(长度为0)
|
||
/// </summary>
|
||
/// <param name="argName">数组名</param>
|
||
/// <param name="argValue">被检查的数组实例</param>
|
||
/// <param name="throwError">为空引用时是否抛出异常</param>
|
||
/// <returns>为空则返回true</returns>
|
||
public static bool CheckEmptyArray(string argName, Array argValue, bool throwError) {
|
||
if(argName == null) {
|
||
throw new ArgumentNullException("argName");
|
||
}
|
||
bool ret = (argValue == null || argValue.Length == 0);
|
||
if(ret && throwError) {
|
||
throw new ArgumentException("数组为空", argName);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断某值是否在枚举内(位枚举)
|
||
/// </summary>
|
||
/// <param name="checkingValue">被检测的枚举值</param>
|
||
/// <param name="expectedValue">期望的枚举值</param>
|
||
/// <returns>是否包含</returns>
|
||
public static bool CheckFlagsEnumEquals(Enum checkingValue, Enum expectedValue) {
|
||
int intCheckingValue = Convert.ToInt32(checkingValue);
|
||
int intExpectedValue = Convert.ToInt32(expectedValue);
|
||
return (intCheckingValue & intExpectedValue) == intExpectedValue;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断是否为base64字符串
|
||
/// </summary>
|
||
/// <param name="str"></param>
|
||
/// <returns></returns>
|
||
public static bool IsBase64String(string str)
|
||
{
|
||
//A-Z, a-z, 0-9, +, /, =
|
||
return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
|
||
}
|
||
/// <summary>
|
||
/// 检测是否有Sql危险字符
|
||
/// </summary>
|
||
/// <param name="str">要判断字符串</param>
|
||
/// <returns>判断结果</returns>
|
||
public static bool IsSafeSqlString(string str)
|
||
{
|
||
|
||
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断文件名是否为浏览器可以直接显示的图片文件名
|
||
/// </summary>
|
||
/// <param name="filename">文件名</param>
|
||
/// <returns>是否可以直接显示</returns>
|
||
public static bool IsImgFilename(string filename)
|
||
{
|
||
filename = filename.Trim();
|
||
if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
|
||
{
|
||
return false;
|
||
}
|
||
string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
|
||
return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否为ip
|
||
/// </summary>
|
||
/// <param name="ip"></param>
|
||
/// <returns></returns>
|
||
public static bool IsIP(string ip)
|
||
{
|
||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
||
|
||
}
|
||
|
||
|
||
public static bool IsIPSect(string ip)
|
||
{
|
||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");
|
||
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 返回指定IP是否在指定的IP数组所限定的范围内, IP数组内的IP地址可以使用*表示该IP段任意, 例如192.168.1.*
|
||
/// </summary>
|
||
/// <param name="ip"></param>
|
||
/// <param name="iparray"></param>
|
||
/// <returns></returns>
|
||
public static bool InIPArray(string ip, string[] iparray)
|
||
{
|
||
|
||
string[] userip = StringHelper.SplitString(ip, @".");
|
||
for (int ipIndex = 0; ipIndex < iparray.Length; ipIndex++)
|
||
{
|
||
string[] tmpip = StringHelper.SplitString(iparray[ipIndex], @".");
|
||
int r = 0;
|
||
for (int i = 0; i < tmpip.Length; i++)
|
||
{
|
||
if (tmpip[i] == "*")
|
||
{
|
||
return true;
|
||
}
|
||
|
||
if (userip.Length > i)
|
||
{
|
||
if (tmpip[i] == userip[i])
|
||
{
|
||
r++;
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
|
||
}
|
||
if (r == 4)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
|
||
}
|
||
return false;
|
||
|
||
}
|
||
public static bool IsDateFormat(string date)
|
||
{
|
||
return Regex.IsMatch(date, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
|
||
}
|
||
}
|
||
}
|