feat:上线服务监控功能
This commit is contained in:
@@ -0,0 +1,446 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 转换为long
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static long ParseToLong(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static long ParseToLong(this string str, long defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为int
|
||||
/// <summary>
|
||||
/// 将object转换为int,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static int ParseToInt(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
|
||||
/// null返回默认值
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static int ParseToInt(this object str, int defaultValue)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为short
|
||||
/// <summary>
|
||||
/// 将object转换为short,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ParseToShort(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return short.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为short,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ParseToShort(this object str, short defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return short.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为demical
|
||||
/// <summary>
|
||||
/// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ParseToDecimal(this object str, decimal defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decimal.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为demical,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ParseToDecimal(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decimal.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转化为bool
|
||||
/// <summary>
|
||||
/// 将object转换为bool,若转换失败,则返回false。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseToBool(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bool.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseToBool(this object str, bool result)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bool.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为float
|
||||
/// <summary>
|
||||
/// 将object转换为float,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static float ParseToFloat(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return float.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为float,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static float ParseToFloat(this object str, float result)
|
||||
{
|
||||
try
|
||||
{
|
||||
return float.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为Guid
|
||||
/// <summary>
|
||||
/// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Guid ParseToGuid(this string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Guid(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Guid.Empty;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为DateTime
|
||||
/// <summary>
|
||||
/// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ParseToDateTime(this string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
if (str.Contains("-") || str.Contains("/"))
|
||||
{
|
||||
return DateTime.Parse(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = str.Length;
|
||||
switch (length)
|
||||
{
|
||||
case 4:
|
||||
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 6:
|
||||
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 8:
|
||||
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 10:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 12:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 14:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
default:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将string转换为DateTime,若转换失败,则返回默认值。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return defaultValue.GetValueOrDefault();
|
||||
}
|
||||
if (str.Contains("-") || str.Contains("/"))
|
||||
{
|
||||
return DateTime.Parse(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = str.Length;
|
||||
switch (length)
|
||||
{
|
||||
case 4:
|
||||
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 6:
|
||||
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 8:
|
||||
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 10:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 12:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 14:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
default:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为string
|
||||
/// <summary>
|
||||
/// 将object转换为string,若转换失败,则返回""。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ParseToString(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
public static string ParseToStrings<T>(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = obj as IEnumerable<T>;
|
||||
if (list != null)
|
||||
{
|
||||
return string.Join(",", list);
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为double
|
||||
/// <summary>
|
||||
/// 将object转换为double,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static double ParseToDouble(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return double.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static double ParseToDouble(this object str, double defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return double.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 强制转换类型
|
||||
/// <summary>
|
||||
/// 强制转换类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
|
||||
{
|
||||
foreach (object item in source)
|
||||
{
|
||||
yield return (TResult)Convert.ChangeType(item, typeof(TResult));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
//using Newtonsoft.Json;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 枚举成员转成dictionary类型
|
||||
/// <summary>
|
||||
/// 转成dictionary类型
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<int, string> EnumToDictionary(this Type enumType)
|
||||
{
|
||||
Dictionary<int, string> dictionary = new Dictionary<int, string>();
|
||||
Type typeDescription = typeof(DescriptionAttribute);
|
||||
FieldInfo[] fields = enumType.GetFields();
|
||||
int sValue = 0;
|
||||
string sText = string.Empty;
|
||||
foreach (FieldInfo field in fields)
|
||||
{
|
||||
if (field.FieldType.IsEnum)
|
||||
{
|
||||
sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
|
||||
object[] arr = field.GetCustomAttributes(typeDescription, true);
|
||||
if (arr.Length > 0)
|
||||
{
|
||||
DescriptionAttribute da = (DescriptionAttribute)arr[0];
|
||||
sText = da.Description;
|
||||
}
|
||||
else
|
||||
{
|
||||
sText = field.Name;
|
||||
}
|
||||
dictionary.Add(sValue, sText);
|
||||
}
|
||||
}
|
||||
return dictionary;
|
||||
}
|
||||
/// <summary>
|
||||
/// 枚举成员转成键值对Json字符串
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
//public static string EnumToDictionaryString(this Type enumType)
|
||||
//{
|
||||
// List<KeyValuePair<int, string>> dictionaryList = EnumToDictionary(enumType).ToList();
|
||||
// var sJson = JsonConvert.SerializeObject(dictionaryList);
|
||||
// return sJson;
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 获取枚举的描述
|
||||
/// <summary>
|
||||
/// 获取枚举值对应的描述
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDescription(this System.Enum enumType)
|
||||
{
|
||||
FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
|
||||
if (EnumInfo != null)
|
||||
{
|
||||
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (EnumAttributes.Length > 0)
|
||||
{
|
||||
return EnumAttributes[0].Description;
|
||||
}
|
||||
}
|
||||
return enumType.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 根据值获取枚举的描述
|
||||
public static string GetDescriptionByEnum<T>(this object obj)
|
||||
{
|
||||
var tEnum = System.Enum.Parse(typeof(T), obj.ParseToString()) as System.Enum;
|
||||
var description = tEnum.GetDescription();
|
||||
return description;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
public static Exception GetOriginalException(this Exception ex)
|
||||
{
|
||||
if (ex.InnerException == null) return ex;
|
||||
|
||||
return ex.InnerException.GetOriginalException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static class LinqExtensions
|
||||
{
|
||||
public static Expression Property(this Expression expression, string propertyName)
|
||||
{
|
||||
return Expression.Property(expression, propertyName);
|
||||
}
|
||||
public static Expression AndAlso(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.AndAlso(left, right);
|
||||
}
|
||||
public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
|
||||
{
|
||||
return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
|
||||
}
|
||||
public static Expression GreaterThan(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.GreaterThan(left, right);
|
||||
}
|
||||
public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
|
||||
{
|
||||
return Expression.Lambda<T>(body, parameters);
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> True<T>() { return param => true; }
|
||||
|
||||
public static Expression<Func<T, bool>> False<T>() { return param => false; }
|
||||
|
||||
/// <summary>
|
||||
/// 组合And
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, Expression.AndAlso);
|
||||
}
|
||||
/// <summary>
|
||||
/// 组合Or
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, Expression.OrElse);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines the first expression with the second using the specified merge function.
|
||||
/// </summary>
|
||||
static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
|
||||
{
|
||||
var map = first.Parameters
|
||||
.Select((f, i) => new { f, s = second.Parameters[i] })
|
||||
.ToDictionary(p => p.s, p => p.f);
|
||||
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
|
||||
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ParameterRebinder
|
||||
/// </summary>
|
||||
private class ParameterRebinder : ExpressionVisitor
|
||||
{
|
||||
/// <summary>
|
||||
/// The ParameterExpression map
|
||||
/// </summary>
|
||||
readonly Dictionary<ParameterExpression, ParameterExpression> map;
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="map">The map.</param>
|
||||
ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
|
||||
{
|
||||
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Replaces the parameters.
|
||||
/// </summary>
|
||||
/// <param name="map">The map.</param>
|
||||
/// <param name="exp">The exp.</param>
|
||||
/// <returns>Expression</returns>
|
||||
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
|
||||
{
|
||||
return new ParameterRebinder(map).Visit(exp);
|
||||
}
|
||||
/// <summary>
|
||||
/// Visits the parameter.
|
||||
/// </summary>
|
||||
/// <param name="p">The p.</param>
|
||||
/// <returns>Expression</returns>
|
||||
protected override Expression VisitParameter(ParameterExpression p)
|
||||
{
|
||||
ParameterExpression replacement;
|
||||
|
||||
if (map.TryGetValue(p, out replacement))
|
||||
{
|
||||
p = replacement;
|
||||
}
|
||||
return base.VisitParameter(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
public static bool IsEmpty(this object value)
|
||||
{
|
||||
if (value != null && !string.IsNullOrEmpty(value.ParseToString()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public static bool IsNotEmpty(this object value)
|
||||
{
|
||||
return !IsEmpty(value);
|
||||
}
|
||||
public static bool IsNullOrZero(this object value)
|
||||
{
|
||||
if (value == null || value.ParseToString().Trim() == "0")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//public static bool IsAjaxRequest(this HttpRequest request)
|
||||
//{
|
||||
// if (request == null)
|
||||
// throw new ArgumentNullException("request");
|
||||
|
||||
// if (request.Headers != null)
|
||||
// return request.Headers["X-Requested-With"] == "XMLHttpRequest";
|
||||
// return false;
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static class StringExtension
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// SQL条件拼接
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="condition"></param>
|
||||
/// <returns></returns>
|
||||
public static string If(this string str, bool condition)
|
||||
{
|
||||
return condition ? str : string.Empty;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断是否为空
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IfNotEmpty(this string str)
|
||||
{
|
||||
return !string.IsNullOrEmpty(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//(");
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="oldValue"></param>
|
||||
/// <param name="newValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string ReplaceFirst(this string input, string oldValue, string newValue)
|
||||
{
|
||||
Regex regEx = new Regex(oldValue, RegexOptions.Multiline);
|
||||
return regEx.Replace(input, newValue == null ? "" : newValue, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 骆驼峰转下划线
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToSmallCamelCase(string name)
|
||||
{
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append(name.Substring(0, 1).ToLower());
|
||||
|
||||
for (var i = 0; i < name.Length; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
stringBuilder.Append(name.Substring(0, 1).ToLower());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (name[i] >= 'A' && name[i] <= 'Z')
|
||||
{
|
||||
stringBuilder.Append($"_{name.Substring(i, 1).ToLower()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.Append(name[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下划线命名转驼峰命名
|
||||
/// </summary>
|
||||
/// <param name="underscore"></param>
|
||||
/// <returns></returns>
|
||||
public static string UnderScoreToCamelCase(this string underscore)
|
||||
{
|
||||
string[] ss = underscore.Split("_");
|
||||
if (ss.Length == 1)
|
||||
{
|
||||
return underscore;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append(ss[0]);
|
||||
for (int i = 1; i < ss.Length; i++)
|
||||
{
|
||||
sb.Append(ss[i].FirstUpperCase());
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 首字母转大写
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string FirstUpperCase(this string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToUpper() + str[1..];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 首字母转小写
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string FirstLowerCase(this string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 截取指定字符串中间内容
|
||||
/// </summary>
|
||||
/// <param name="sourse"></param>
|
||||
/// <param name="startstr"></param>
|
||||
/// <param name="endstr"></param>
|
||||
/// <returns></returns>
|
||||
public static string SubstringBetween(this string sourse, string startstr, string endstr)
|
||||
{
|
||||
string result = string.Empty;
|
||||
int startindex, endindex;
|
||||
try
|
||||
{
|
||||
startindex = sourse.IndexOf(startstr);
|
||||
if (startindex == -1)
|
||||
return result;
|
||||
string tmpstr = sourse.Substring(startindex + startstr.Length);
|
||||
endindex = tmpstr.IndexOf(endstr);
|
||||
if (endindex == -1)
|
||||
return result;
|
||||
result = tmpstr.Remove(endindex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("MidStrEx Err:" + ex.Message);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为Pascal风格-每一个单词的首字母大写
|
||||
/// </summary>
|
||||
/// <param name="fieldName">字段名</param>
|
||||
/// <param name="fieldDelimiter">分隔符</param>
|
||||
/// <returns></returns>
|
||||
public static string ConvertToPascal(this string fieldName, string fieldDelimiter)
|
||||
{
|
||||
string result = string.Empty;
|
||||
if (fieldName.Contains(fieldDelimiter))
|
||||
{
|
||||
//全部小写
|
||||
string[] array = fieldName.ToLower().Split(fieldDelimiter.ToCharArray());
|
||||
foreach (var t in array)
|
||||
{
|
||||
//首字母大写
|
||||
result += t.Substring(0, 1).ToUpper() + t[1..];
|
||||
}
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(fieldName))
|
||||
{
|
||||
result = fieldName;
|
||||
}
|
||||
else if (fieldName.Length == 1)
|
||||
{
|
||||
result = fieldName.ToUpper();
|
||||
}
|
||||
else if (fieldName.Length == fieldName.CountUpper())
|
||||
{
|
||||
result = fieldName.Substring(0, 1).ToUpper() + fieldName[1..].ToLower();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = fieldName.Substring(0, 1).ToUpper() + fieldName[1..];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 大写字母个数
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static int CountUpper(this string str)
|
||||
{
|
||||
int count1 = 0;
|
||||
char[] chars = str.ToCharArray();
|
||||
foreach (char num in chars)
|
||||
{
|
||||
if (num >= 'A' && num <= 'Z')
|
||||
{
|
||||
count1++;
|
||||
}
|
||||
//else if (num >= 'a' && num <= 'z')
|
||||
//{
|
||||
// count2++;
|
||||
//}
|
||||
}
|
||||
return count1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为Camel风格-第一个单词小写,其后每个单词首字母大写
|
||||
/// </summary>
|
||||
/// <param name="fieldName">字段名</param>
|
||||
/// <param name="fieldDelimiter">分隔符</param>
|
||||
/// <returns></returns>
|
||||
public static string ConvertToCamel(this string fieldName, string fieldDelimiter)
|
||||
{
|
||||
//先Pascal
|
||||
string result = fieldName.ConvertToPascal(fieldDelimiter);
|
||||
//然后首字母小写
|
||||
if (result.Length == 1)
|
||||
{
|
||||
result = result.ToLower();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = result.Substring(0, 1).ToLower() + result[1..];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user