增加老版本 KJDWebService

This commit is contained in:
chfei committed 2026-08-21 16:46:36 +08:00
1 parent 58abbb5b4f
commit f84c4c1949
115 files changed
+36598 -18

No files matched your search

@@ -0,0 +1,705 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using NaiBa.Util.Helper;
using Newtonsoft.Json;
namespace NaiBa.Util.Extension
{
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)
{
if (obj == null)
return 0L;
try
{
string str = obj.ParseToString();
if (string.IsNullOrEmpty(str))
{
return 0;
}
return long.Parse(str);
}
catch
{
return 0L;
}
}
public static long? ParseToNullableLong(this object obj)
{
if (obj == null)
return null;
try
{
string str = obj.ParseToString();
if (string.IsNullOrEmpty(str))
{
return null;
}
return long.Parse(str);
}
catch
{
return null;
}
}
/// <summary>
/// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
/// </summary>
/// <param name="str"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long ParseToLong(this string str, long defaultValue)
{
if (str == null)
return 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)
{
if (str == null)
return 0;
try
{
return Convert.ToInt32(str);
}
catch
{
return 0;
}
}
public static int? ParseToNullableInt(this object str)
{
if (str == null)
return null;
try
{
return Convert.ToInt32(str);
}
catch
{
return null;
}
}
public static int DecimalPointParseToInt(this object str)
{
try
{
string temp = str.ParseToString();
int i = temp.IndexOf('.');
if (i > 0)
{
temp = temp.Substring(0, i);
}
return Convert.ToInt32(temp);
}
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;
}
}
public static decimal? ParseToNullableDecimal(this object str)
{
try
{
return decimal.Parse(str.ToString());
}
catch
{
return null;
}
}
#endregion
#region 转化为bool
/// <summary>
/// 将object转换为bool,若转换失败,则返回false。不抛出异常。
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool ParseToBool(this object str)
{
try
{
if (str is null)
return false;
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
public static DateTime? ParseToDateTimeNullable(this string str)
{
try
{
if (string.IsNullOrWhiteSpace(str))
{
return null;
}
try
{
return DateTime.Parse(str);
}
catch
{
}
if (str.Contains("年") && str.Contains("月") && str.Contains("日") && !str.Contains("周"))
str = str.Replace("年", "-").Replace("月", "-").Replace("日", "").Replace("时", ":").Replace("分", ":").Replace("秒", "").Trim(' ', ':');
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 null;
}
}
/// <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
{
if (obj is DateTime)
return (obj as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss");
return obj.ToString();
}
}
catch
{
return string.Empty;
}
}
public static string ParseToNullableString(this object obj)
{
try
{
if (obj == null)
{
return null;
}
else
{
if (obj is DateTime)
return (obj as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss");
return obj.ToString();
}
}
catch
{
return null;
}
}
public static string ParseToStrings<T>(this object obj, string split = ",")
{
try
{
var list = obj as IEnumerable<T>;
if (list != null)
{
return string.Join(split, 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 字符串转指定类型Array
public static T[] SplitToArray<T>(this string value, string split = ",")
{
if (string.IsNullOrEmpty(value))
{
return default(T[]);
}
var arr = value.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries).CastSuper<T>().ToArray();
return arr;
}
public static T[] SplitToArray<T>(this string value, string[] split)
{
if (string.IsNullOrEmpty(value))
{
return default(T[]);
}
var arr = value.Split(split, StringSplitOptions.RemoveEmptyEntries).CastSuper<T>().ToArray();
return arr;
}
#endregion
#region 字符串转指定类型List
public static List<T> SplitToList<T>(this string value, string split = ",")
{
if (string.IsNullOrEmpty(value))
{
return new List<T>();
}
var list = value.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries).CastSuper<T>().ToList();
return list;
}
public static List<T> SplitToList<T>(this string value, string[] split)
{
if (string.IsNullOrEmpty(value))
{
return new List<T>();
}
var list = value.Split(split, StringSplitOptions.RemoveEmptyEntries).CastSuper<T>().ToList();
return list;
}
#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
#region Object转换为Json
public static string ParseToJson(this object obj)
{
return JsonConvert.SerializeObject(obj);
}
#endregion
#region Json转为Object
public static T ParseToObject<T>(this string json)
{
if (string.IsNullOrEmpty(json))
{
return Activator.CreateInstance<T>();
}
return JsonConvert.DeserializeObject<T>(json);
}
#endregion
#region 深拷贝(克隆出一个新对象,拷贝对象里面的数据和子对象)
public static T DeepClone<T>(this T o)
{
return o.ParseToJson().ParseToObject<T>();
// 这种方式在用 Obfuscator 的时候有问题
//using (MemoryStream stream = new MemoryStream())
//{
// BinaryFormatter formatter = new BinaryFormatter();
// formatter.Serialize(stream, o);
// stream.Position = 0;
// return (T)formatter.Deserialize(stream);
//}
}
#endregion
#region 浅拷贝(拷贝对象里面的数据[property and field],但不拷贝对象里面的子对象)
public static void ShadowClone<T1, T2>(this T1 source, ref T2 destination)
{
var sourceMembers = ReflectionHelper.GetMembers(source.GetType());
var destinationMembers = ReflectionHelper.GetMembers(destination.GetType());
// Copy data from source to destination
foreach (var sourceMember in sourceMembers)
{
if (!ReflectionHelper.CanRead(sourceMember))
{
continue;
}
var destinationMember = destinationMembers.FirstOrDefault(x => x.Name.ToLower() == sourceMember.Name.ToLower());
if (destinationMember == null || !ReflectionHelper.CanWrite(destinationMember))
{
continue;
}
ReflectionHelper.SetObjectValue(ref destination, destinationMember, ReflectionHelper.GetMemberValue(source, sourceMember));
}
}
#endregion
}
}
@@ -0,0 +1,114 @@
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 NaiBa.Util.Extension
{
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.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value);
}
/// <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
#region 根据描述获取枚举值
public static int GetEnumValueByDescription<T>(this string description)
{
Dictionary<int, string> dict = typeof(T).EnumToDictionary();
int value = dict.Where(p => p.Value == description).Select(p => p.Key).FirstOrDefault();
return value;
}
#endregion
#region 根据枚举名称获取值
public static int GetEnumValueByName<T>(this string name)
{
return (int)System.Enum.Parse(typeof(T), name);
}
#endregion
#region 根据枚举值获取名称
public static object GetEnumNameByValue<T>(this int value)
{
return System.Enum.ToObject(typeof(T), value);
}
#endregion
}
}
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NaiBa.Util.Extension
{
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 NaiBa.Util.Extension
{
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,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using NaiBa.Util.Helper;
using Newtonsoft.Json.Linq;
namespace NaiBa.Util.Extension
{
public static partial class Extensions
{
public static bool IsNullOrEmpty(this object value)
{
if (value != null && !string.IsNullOrEmpty(value.ParseToString()))
{
return false;
}
else
{
return true;
}
}
public static bool IsNullOrZero(this object value)
{
if (value == null || string.IsNullOrEmpty(value.ParseToString()) || value.ParseToString().Trim() == "0")
{
return true;
}
else
{
return false;
}
}
public static bool IsNumericType(this object value)
{
if (value == null)
{
return false;
}
switch (Type.GetTypeCode(value.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
public static bool IsStringType(this object value)
{
if (value == null)
{
return false;
}
switch (Type.GetTypeCode(value.GetType()))
{
case TypeCode.String:
return true;
default:
return false;
}
}
public static bool IsNumber(this string value)
{
return RegexHelper.IsNumber(value);
}
public static bool IsAlphabet(this string value)
{
return RegexHelper.IsAlphabet(value);
}
public static bool IsAlphabetOrNumber(this string value)
{
return RegexHelper.IsAlphabetOrNumber(value);
}
public static bool IsJson(this string value)
{
return JsonHelper.IsJson(value);
}
public static bool IsJTokenNullOrEmpty(this JToken token)
{
return (token == null) ||
(token.Type == JTokenType.Array && !token.HasValues) ||
(token.Type == JTokenType.Object && !token.HasValues) ||
(token.Type == JTokenType.String && token.ToString() == string.Empty) ||
(token.Type == JTokenType.Null);
}
public static bool IsAjaxRequest(this Microsoft.AspNetCore.Http.HttpRequest request)
{
if (request == null)
throw new ArgumentNullException("request");
if (request.Headers != null)
return request.Headers["X-Requested-With"] == "XMLHttpRequest";
return false;
}
}
}