初始提交
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
/// <summary>
|
||||
/// 程序集扩展类
|
||||
/// </summary>
|
||||
public static class AssemblyExtension
|
||||
{
|
||||
/// <summary>
|
||||
///得到程序集友好名字
|
||||
/// </summary>
|
||||
/// <param name="para"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFriendName(this Assembly asm)
|
||||
{
|
||||
return asm.ManifestModule?.Name?.TrimEnd(".dll".ToCharArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
/// <summary>
|
||||
/// bool类型扩展
|
||||
/// </summary>
|
||||
public static class BoolExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 转为bool
|
||||
/// </summary>
|
||||
/// <param name="para"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ToBool(this bool? para)
|
||||
{
|
||||
if (para == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Convert.ToBoolean(para);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 转为bool
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ToBool(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool.TryParse(obj.ToString(), out var para);
|
||||
|
||||
return para;
|
||||
}
|
||||
|
||||
public static bool ToBool(this sbyte? obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
sbyte num = (sbyte) obj;
|
||||
|
||||
if (num == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
using System;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class DateTimeExtension
|
||||
{
|
||||
public static string Format(this DateTime time, string format = "yyyy-MM-dd")
|
||||
{
|
||||
return time.ToString(format);
|
||||
}
|
||||
|
||||
|
||||
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
public static long CurrentTimeMillis()
|
||||
{
|
||||
return (long) ((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
|
||||
}
|
||||
|
||||
public static long CurrentTimeMillis(DateTime time)
|
||||
{
|
||||
return (long) ((time.ToUniversalTime() - Jan1st1970).TotalMilliseconds);
|
||||
}
|
||||
|
||||
public static string Format(this DateTime? dateTime, string format = "yyyy-MM-dd")
|
||||
{
|
||||
return Convert.ToDateTime(dateTime).ToString(format);
|
||||
}
|
||||
|
||||
public static DateTime ToDateTime(this DateTime? dateTime, DateTime defaultTime)
|
||||
{
|
||||
if (dateTime != null)
|
||||
{
|
||||
return Convert.ToDateTime(dateTime);
|
||||
}
|
||||
|
||||
return defaultTime;
|
||||
}
|
||||
|
||||
public static bool Between(this DateTime time, DateTime beginTime, DateTime endTime)
|
||||
{
|
||||
return time >= beginTime && time <= endTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取得某月的第一天
|
||||
/// </summary>
|
||||
/// <param name="datetime">要取得月份第一天的时间</param>
|
||||
/// <returns></returns>
|
||||
public static DateTime FirstDayOfMonth(this DateTime datetime)
|
||||
{
|
||||
return datetime.AddDays(1 - datetime.Day);
|
||||
}
|
||||
|
||||
/**/
|
||||
/// <summary>
|
||||
/// 取得某月的最后一天
|
||||
/// </summary>
|
||||
/// <param name="datetime">要取得月份最后一天的时间</param>
|
||||
/// <returns></returns>
|
||||
public static DateTime LastDayOfMonth(this DateTime datetime)
|
||||
{
|
||||
return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
|
||||
}
|
||||
|
||||
/**/
|
||||
/// <summary>
|
||||
/// 取得上个月第一天
|
||||
/// </summary>
|
||||
/// <param name="datetime">要取得上个月第一天的当前时间</param>
|
||||
/// <returns></returns>
|
||||
public static DateTime FirstDayOfPreviousMonth(this DateTime datetime)
|
||||
{
|
||||
return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
|
||||
}
|
||||
|
||||
/**/
|
||||
/// <summary>
|
||||
/// 取得上个月的最后一天
|
||||
/// </summary>
|
||||
/// <param name="datetime">要取得上个月最后一天的当前时间</param>
|
||||
/// <returns></returns>
|
||||
public static DateTime LastDayOfPrdviousMonth(this DateTime datetime)
|
||||
{
|
||||
return datetime.AddDays(1 - datetime.Day).AddDays(-1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取时间的Unix时间戳
|
||||
/// </summary>
|
||||
/// <param name="tm">时间对象</param>
|
||||
/// <returns>Unix时间戳</returns>
|
||||
///
|
||||
public static long GetUnixTimeStamp(this DateTime tm)
|
||||
{
|
||||
long result = (tm.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Uninx转换时间
|
||||
/// </summary>
|
||||
/// <param name="tm">时间对象</param>
|
||||
/// <param name="timeStamp">时间戳</param>
|
||||
/// <returns>新时间对象</returns>
|
||||
///
|
||||
public static DateTime LoadFromUnixTimeStamp(this DateTime tm,double timeStamp)
|
||||
{
|
||||
DateTime startTime=TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1),TimeZoneInfo.Local);
|
||||
DateTime result=startTime.AddSeconds(timeStamp);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static DateTime LoadFromUnixTimeStamp(this long timeStamp)
|
||||
{
|
||||
DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), TimeZoneInfo.Local);
|
||||
DateTime result = startTime.AddSeconds(timeStamp);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 时间戳转换成时间
|
||||
/// </summary>
|
||||
/// <param name="timestamp">时间戳</param>
|
||||
/// <param name="millisecond">是否毫秒级,true毫秒级(默认值)</param>
|
||||
/// <param name="localTime">是否输出本地时间,true本地时间(默认值)</param>
|
||||
/// <returns></returns>
|
||||
public static DateTime? LoadFromUnixTimeStamp(this string timestamp)
|
||||
{
|
||||
if (long.TryParse(timestamp, out long ts))
|
||||
{
|
||||
return ts.LoadFromUnixTimeStamp();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1970 到现在的秒数
|
||||
/// </summary>
|
||||
/// <param name="time"></param>
|
||||
/// <returns></returns>
|
||||
public static int TimestampFrom19700101(this DateTime time)
|
||||
{
|
||||
return (int)(time - new DateTime(1970, 01, 01)).TotalSeconds;
|
||||
}
|
||||
|
||||
|
||||
#region 获取日期、明天
|
||||
public static DateTime Date(this DateTime? time)
|
||||
{
|
||||
return Convert.ToDateTime(time).Date;
|
||||
}
|
||||
public static DateTime NextDate(this DateTime? time)
|
||||
{
|
||||
return Convert.ToDateTime(time).Date.AddDays(1);
|
||||
}
|
||||
public static DateTime Date(this DateTime time)
|
||||
{
|
||||
return time.Date;
|
||||
}
|
||||
public static DateTime NextDate(this DateTime time)
|
||||
{
|
||||
return time.Date.AddDays(1);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static DateTime Begin(this DateTime time)
|
||||
{
|
||||
return new DateTime(time.Year, time.Month, time.Day);
|
||||
}
|
||||
|
||||
public static DateTime End(this DateTime time)
|
||||
{
|
||||
return new DateTime(time.Year, time.Month, time.Day,23,59,59);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Dynamic;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class DbDataReaderExtension
|
||||
{
|
||||
public static IDictionary<string, object> GetDataRow(this DbDataReader dataReader)
|
||||
{
|
||||
var dataRow = new ExpandoObject() as IDictionary<string, object>;
|
||||
|
||||
for (var iFiled = 0; iFiled < dataReader.FieldCount; iFiled++)
|
||||
{
|
||||
dataRow.Add(
|
||||
dataReader.GetName(iFiled),
|
||||
dataReader.IsDBNull(iFiled) ? "" : dataReader[iFiled]
|
||||
);
|
||||
}
|
||||
|
||||
return dataRow;
|
||||
}
|
||||
}
|
||||
}
|
||||
149
Infrastructure/Hncore.Infrastructure/Extension/EnumExtension.cs
Normal file
149
Infrastructure/Hncore.Infrastructure/Extension/EnumExtension.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class EnumExtension
|
||||
{
|
||||
public static Dictionary<int, string> ToDictionary<T>()
|
||||
{
|
||||
Dictionary<int, string> dic = new Dictionary<int, string>();
|
||||
string namestr = "";
|
||||
foreach (var e in Enum.GetValues(typeof(T)))
|
||||
{
|
||||
namestr = "";
|
||||
|
||||
object[] objArrDisplay = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DisplayAttribute), true);//Display
|
||||
if (objArrDisplay.Any())
|
||||
{
|
||||
var da = objArrDisplay[0] as DisplayAttribute;
|
||||
namestr = da.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
object[] objArrDescription = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);//Description
|
||||
namestr = objArrDescription.Any() ? (objArrDescription[0] as DescriptionAttribute).Description : e.ToString();
|
||||
}
|
||||
int value = Convert.ToInt32(e);
|
||||
|
||||
|
||||
|
||||
// string str = item.GetDescription();
|
||||
// int value = (int) item;
|
||||
|
||||
dic.Add(value, namestr);
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
#region 获取枚举的相关信息的集合
|
||||
/// <summary>
|
||||
/// 获取枚举的相关信息的集合
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<EnumInfo> EnumToList<T>()
|
||||
{
|
||||
var list = new List<EnumInfo>();
|
||||
foreach (var e in Enum.GetValues(typeof(T)))
|
||||
{
|
||||
var m = new EnumInfo();
|
||||
object[] objArrDisplay = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DisplayAttribute), true);//Display
|
||||
if (objArrDisplay.Any())
|
||||
{
|
||||
var da = objArrDisplay[0] as DisplayAttribute;
|
||||
m.Name = da.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
object[] objArrDescription = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);//Description
|
||||
m.Name = objArrDescription.Any() ? (objArrDescription[0] as DescriptionAttribute).Description : e.ToString();
|
||||
}
|
||||
m.Value = Convert.ToInt32(e);
|
||||
list.Add(m);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
public class EnumInfo
|
||||
{
|
||||
public string Name { set; get; }
|
||||
|
||||
public int Value { set; get; }
|
||||
}
|
||||
#endregion
|
||||
public static string GetDisplayName<T>(T enumValue)
|
||||
{
|
||||
object[] objArrDisplay = enumValue.GetType().GetField(enumValue.ToString())
|
||||
?.GetCustomAttributes(typeof(DisplayAttribute), true);//Display
|
||||
if (objArrDisplay!=null&&objArrDisplay.Any())
|
||||
{
|
||||
var da = objArrDisplay[0] as DisplayAttribute;
|
||||
return da?.Name;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取枚举上通过DisplayName、Description或Display柱注的名称
|
||||
/// 优先级为DisplayName>Description>Display
|
||||
/// </summary>
|
||||
/// <param name="e">枚举值</param>
|
||||
/// <returns>枚举名称</returns>
|
||||
///
|
||||
public static string GetEnumDisplayName(this Enum e)
|
||||
{
|
||||
try
|
||||
{
|
||||
Type t = e.GetType();
|
||||
FieldInfo fi = t.GetField(Enum.GetName(t, e));
|
||||
var dna = fi.GetCustomAttribute<DisplayNameAttribute>();
|
||||
if (dna != null)
|
||||
return dna.DisplayName;
|
||||
var da = fi.GetCustomAttribute<DescriptionAttribute>();
|
||||
if (da != null)
|
||||
return da.Description;
|
||||
var d = fi.GetCustomAttribute<DisplayAttribute>();
|
||||
if (d != null)
|
||||
return d.Name;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return "获取枚举"+e.GetType().FullName+"名称错误:"+ex.Message;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static string ToHtmlSelectOptions<T>()
|
||||
{
|
||||
var dic = ToDictionary<T>();
|
||||
|
||||
string str = "";
|
||||
|
||||
foreach (var key in dic.Keys)
|
||||
{
|
||||
str += "<option value=\"" + key + "\">" + dic[key] + "</option>";
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
#region 判断值是否在枚举类型中存在
|
||||
|
||||
/// <summary>
|
||||
/// 判断值是否在枚举中存在
|
||||
/// </summary>
|
||||
/// <param name="enumValue">需要判断的参数</param>
|
||||
/// <param name="enumType">枚举类型</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsExist(this int enumValue, Type enumType)
|
||||
{
|
||||
return Enum.IsDefined(enumType, enumValue);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using TinyPinyin.Core;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class ExceptionExtension
|
||||
{
|
||||
public static string GetInfo(this Exception ex)
|
||||
{
|
||||
var info = $"S:{ex.Source},M:{ex.Message},ST:{ex.StackTrace}-----";
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
info += ex.InnerException.GetInfo();
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Hncore.Infrastructure.Serializer;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class HttpClientFactoryExtension
|
||||
{
|
||||
public static HttpClient CreateClient(this IHttpClientFactory factory, TimeSpan timeOut)
|
||||
{
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.Timeout = timeOut;
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
public static class HttpClientExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// post请求,ContentType:application/json
|
||||
/// </summary>
|
||||
/// <param name="httpClient"></param>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<HttpResponseMessage> PostAsJson(this HttpClient httpClient, string path, object data,
|
||||
Encoding encoding = null)
|
||||
{
|
||||
if (encoding == null)
|
||||
{
|
||||
encoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
string content = "";
|
||||
|
||||
if (data is string s)
|
||||
{
|
||||
content = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
content = data.ToJson();
|
||||
}
|
||||
|
||||
HttpContent httpContent = new StringContent(content, encoding);
|
||||
|
||||
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
|
||||
return await httpClient.PostAsync(path, httpContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// post请求,ContentType:application/json
|
||||
/// </summary>
|
||||
/// <param name="httpClient"></param>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> PostAsJsonGetString(this HttpClient httpClient, string path, object data,
|
||||
Encoding encoding = null)
|
||||
{
|
||||
var res = await httpClient.PostAsJson(path, data, encoding);
|
||||
|
||||
return await res.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
public static async Task<HttpResponseMessage> PostAsForm(this HttpClient httpClient, string path, IEnumerable<KeyValuePair<string, string>> data,
|
||||
Encoding encoding = null)
|
||||
{
|
||||
if (encoding == null)
|
||||
{
|
||||
encoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
HttpContent httpContent = new FormUrlEncodedContent(data);
|
||||
|
||||
return await httpClient.PostAsync(path, httpContent);
|
||||
}
|
||||
public static async Task<string> PostAsFormGetString(this HttpClient httpClient, string path, IEnumerable<KeyValuePair<string, string>> data,
|
||||
Encoding encoding = null)
|
||||
{
|
||||
var resp=await httpClient.PostAsForm(path, data, encoding);
|
||||
|
||||
return await resp.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
126
Infrastructure/Hncore.Infrastructure/Extension/ListExtension.cs
Normal file
126
Infrastructure/Hncore.Infrastructure/Extension/ListExtension.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class ListExtension
|
||||
{
|
||||
public static bool NullOrEmpty<T>(this IList<T> list)
|
||||
{
|
||||
if (list == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!list.Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#region 转换几个中所有元素的类型
|
||||
/// <summary>
|
||||
/// 转换几个中所有元素的类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TO"></typeparam>
|
||||
/// <param name="list"></param>
|
||||
/// <returns></returns>
|
||||
public static List<TO> ConvertListType<TO, T>(this List<T> list)
|
||||
{
|
||||
if (list == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
List<TO> newlist = new List<TO>();
|
||||
foreach (T t in list)
|
||||
{
|
||||
newlist.Add((TO)Convert.ChangeType(t, typeof(TO)));
|
||||
}
|
||||
return newlist;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 添加
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<T> AddNew<T>(this List<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加多个集合
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<T> AddNewMult<T>(this List<T> list, List<T> item)
|
||||
{
|
||||
list.AddRange(item);
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<T> RemoveNew<T>(this List<T> list, T item)
|
||||
{
|
||||
list.Remove(item);
|
||||
return list;
|
||||
}
|
||||
|
||||
#region 按条件移除
|
||||
|
||||
/// <summary>
|
||||
/// 移除单条符合条件的数据
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="condtion">条件</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<T> RemoveNew<T>(this List<T> list, Func<T, bool> condtion)
|
||||
{
|
||||
List<T> listTemp = list;
|
||||
var item = listTemp.FirstOrDefault(condtion);
|
||||
if (item != null)
|
||||
{
|
||||
listTemp.Remove(item);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除多条满足条件
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="condtion">条件</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<T> RemoveMultNew<T>(this List<T> list, Func<T, bool> condtion)
|
||||
{
|
||||
List<T> listTemp = list;
|
||||
var items = listTemp.Where(condtion).ToList() ?? new List<T>();
|
||||
foreach (var item in items)
|
||||
{
|
||||
listTemp.Remove(item);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class ListForEachExtension
|
||||
{
|
||||
public static async Task ForEachAsync<T>(this IEnumerable<T> list, Func<T, Task> func)
|
||||
{
|
||||
foreach (T value in list)
|
||||
{
|
||||
await func(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class NumberExtension
|
||||
{
|
||||
public static int ToInt(this int? num)
|
||||
{
|
||||
if (num == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Convert.ToInt32(num);
|
||||
}
|
||||
|
||||
public static int ToInt(this JToken obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int.TryParse(obj.ToString(), out var num);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
public static decimal ToDecimal(this decimal? num)
|
||||
{
|
||||
if (num == null)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Convert.ToDecimal(num);
|
||||
}
|
||||
|
||||
public static long ToLong(this long? num)
|
||||
{
|
||||
if (num == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (long) num;
|
||||
}
|
||||
|
||||
public static int ToInt(this bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
using Nelibur.ObjectMapper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class ObjectExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 将对象[主要是匿名对象]转换为dynamic
|
||||
/// </summary>
|
||||
public static dynamic ToDynamic(this object obj, decimal defaultVal)
|
||||
{
|
||||
decimal result;
|
||||
if (obj != null)
|
||||
if (decimal.TryParse(obj.ToString(), out result))
|
||||
return result;
|
||||
else
|
||||
return defaultVal;
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
// This extension method is broken out so you can use a similar pattern with
|
||||
// other MetaData elements in the future. This is your base method for each.
|
||||
public static T GetAttribute<T>(this object value) where T : Attribute
|
||||
{
|
||||
var type = value.GetType();
|
||||
var memberInfo = type.GetMember(value.ToString());
|
||||
if (!memberInfo.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
|
||||
return (T) attributes[0];
|
||||
}
|
||||
|
||||
// This method creates a specific call to the above method, requesting the
|
||||
// Description MetaData attribute.
|
||||
public static string GetDescription(this object value)
|
||||
{
|
||||
var desAttribute = value.GetAttribute<DescriptionAttribute>();
|
||||
if (desAttribute != null)
|
||||
{
|
||||
return desAttribute.Description;
|
||||
}
|
||||
|
||||
var displayAttribute = value.GetAttribute<DisplayAttribute>();
|
||||
if (displayAttribute != null)
|
||||
{
|
||||
return displayAttribute.Name ?? displayAttribute.Description;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
#region 得到枚举字典(key对应枚举的值,value对应枚举的注释)
|
||||
|
||||
/// <summary>
|
||||
/// 得到枚举字典(key对应枚举的值,value对应枚举的注释)
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<Enum, string> ToDescriptionDictionary<TEnum>()
|
||||
{
|
||||
Array values = Enum.GetValues(typeof(TEnum));
|
||||
Dictionary<Enum, string> nums = new Dictionary<Enum, string>();
|
||||
foreach (Enum value in values)
|
||||
{
|
||||
nums.Add(value, GetDescription(value));
|
||||
}
|
||||
|
||||
return nums;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 实体中string属性执行Trim()
|
||||
/// <summary>
|
||||
/// 对象string属性执行Trim()
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static T ObjectStrTrim<T>(this T t) where T:new ()
|
||||
{
|
||||
if (t != null)
|
||||
{
|
||||
foreach (var pi in t.GetType().GetProperties())
|
||||
{
|
||||
if (pi.PropertyType.Equals(typeof(string)) && pi.GetValue(t, null) != null)//判断属性的类型是不是String
|
||||
{
|
||||
pi.SetValue(t, pi.GetValue(t, null).ToString().Trim(), null);//给泛型的属性赋值
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region
|
||||
public static T save<T>(this T obj,string key) where T:class,new()
|
||||
{
|
||||
Dictionary<string, object> dic = c.Value ?? new Dictionary<string, object>();
|
||||
dic.Add(key,obj);
|
||||
c.Value = dic;
|
||||
return obj;
|
||||
}
|
||||
public static AsyncLocal<Dictionary<string,Object>> c=new AsyncLocal<Dictionary<string, Object>>();
|
||||
#endregion
|
||||
|
||||
#region 对象转换
|
||||
|
||||
public static T MapTo<T>(this Object model)
|
||||
{
|
||||
var productDto = TinyMapper.Map<T>(model);
|
||||
|
||||
return productDto;
|
||||
}
|
||||
public static IEnumerable<T> MapsTo<T>(this Object model)
|
||||
{
|
||||
var generic = model.GetType().GetGenericTypeDefinition();
|
||||
|
||||
if (generic == typeof(List<>))
|
||||
{
|
||||
return TinyMapper.Map<List<T>>(model);
|
||||
}
|
||||
if (generic == typeof(Collection<>))
|
||||
{
|
||||
return TinyMapper.Map<Collection<T>>(model);
|
||||
}
|
||||
|
||||
throw new Exception("不合法的转换");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
|
||||
public static class RequestExtension
|
||||
{
|
||||
public static string Get(this HttpRequest request, string key)
|
||||
{
|
||||
if (request.Query.ContainsKey(key))
|
||||
{
|
||||
return request.Query[key];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
public static int GetInt(this HttpRequest request, string key)
|
||||
{
|
||||
if (request.Query.ContainsKey(key))
|
||||
{
|
||||
return Convert.ToInt32(request.Query[key]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static string GetUrl(this HttpRequest request, bool full=true)
|
||||
{
|
||||
if (full)
|
||||
{
|
||||
return $"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}";
|
||||
}
|
||||
return $"{request.Path}{request.QueryString}";
|
||||
}
|
||||
|
||||
public static string Remove(this HttpRequest request, string key)
|
||||
{
|
||||
var q = request.Query.Where(m => !m.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
|
||||
var kvs = q.Select(m => $"{m.Key}={m.Value}");
|
||||
return string.Join("&", kvs);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class StreamExtension
|
||||
{
|
||||
public async static Task<string> ReadAsStringAsync(this Stream stream)
|
||||
{
|
||||
var reader = new StreamReader(stream);
|
||||
return await reader.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,779 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using TinyPinyin.Core;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class StringExtension
|
||||
{
|
||||
#region Url、Html编码
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string UrlEncode(this string target)
|
||||
{
|
||||
return HttpUtility.UrlEncode(target);
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string UrlEncode(this string target, Encoding encoding)
|
||||
{
|
||||
return HttpUtility.UrlEncode(target, encoding);
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string UrlDecode(this string target)
|
||||
{
|
||||
return HttpUtility.UrlDecode(target);
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string UrlDecode(this string target, Encoding encoding)
|
||||
{
|
||||
return HttpUtility.UrlDecode(target, encoding);
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string AttributeEncode(this string target)
|
||||
{
|
||||
return HttpUtility.HtmlAttributeEncode(target);
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string HtmlEncode(this string target)
|
||||
{
|
||||
return HttpUtility.HtmlEncode(target);
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static string HtmlDecode(this string target)
|
||||
{
|
||||
return HttpUtility.HtmlDecode(target);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unicode编码
|
||||
|
||||
/// <summary>
|
||||
/// 汉字转换为Unicode编码
|
||||
/// </summary>
|
||||
/// <param name="str">要编码的汉字字符串</param>
|
||||
/// <returns>Unicode编码的的字符串</returns>
|
||||
public static string ToUnicode(this string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
StringBuilder unicode = new StringBuilder();
|
||||
|
||||
foreach (char chr in str)
|
||||
{
|
||||
// Get the integral value of the character.
|
||||
int value = Convert.ToInt32(chr);
|
||||
// Convert the decimal value to a hexadecimal value in string form.
|
||||
string hexOutput = String.Format("{0:x}", value);
|
||||
|
||||
unicode.Append("\\u" + hexOutput);
|
||||
}
|
||||
|
||||
return unicode.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Unicode编码转换为汉字字符串
|
||||
/// </summary>
|
||||
/// <param name="str">Unicode编码字符串</param>
|
||||
/// <returns>汉字字符串</returns>
|
||||
public static string FromUnicode(this string unicode)
|
||||
{
|
||||
if (string.IsNullOrEmpty(unicode))
|
||||
{
|
||||
return unicode;
|
||||
}
|
||||
|
||||
StringBuilder str = new StringBuilder();
|
||||
|
||||
unicode = unicode.Replace("\\u", "_");
|
||||
|
||||
string[] hex = unicode.Split('_');
|
||||
|
||||
for (int i = 1; i < hex.Length; i++)
|
||||
{
|
||||
int data = Convert.ToInt32(hex[i].ToString(), 16);
|
||||
|
||||
str.Append((char) data);
|
||||
}
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 清除脚本
|
||||
|
||||
/// <summary>
|
||||
/// 清除脚本
|
||||
/// </summary>
|
||||
/// <param name="Htmlstring"></param>
|
||||
/// <returns></returns>
|
||||
public static string NoHTML(this string Htmlstring)
|
||||
{
|
||||
//删除脚本
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
|
||||
//删除HTML
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"<.*?>|&.{4,5}", "", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
|
||||
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
|
||||
Htmlstring.Replace("<", "");
|
||||
Htmlstring.Replace(">", "");
|
||||
Htmlstring.Replace("\r\n", "");
|
||||
Htmlstring = Htmlstring.HtmlEncode().Trim();
|
||||
return Htmlstring;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 清楚小数点后的0
|
||||
|
||||
/// <summary>
|
||||
/// 清楚小数点后的0
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string DecimalToInt(this string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
return "0";
|
||||
str = float.Parse(str).ToString("0.00");
|
||||
if (Int32.Parse(str.Substring(str.IndexOf(".") + 1)) == 0)
|
||||
{
|
||||
return str.Substring(0, str.IndexOf("."));
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static string ToMoney(this decimal num)
|
||||
{
|
||||
return num.ToString("0.00");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 转换成百分比
|
||||
|
||||
/// <summary>
|
||||
/// 转换百分比
|
||||
/// </summary>
|
||||
/// <param name="num"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToP2(this decimal num)
|
||||
{
|
||||
string str = num.ToString("0.00").TrimEnd('0');
|
||||
|
||||
if (str.EndsWith("."))
|
||||
{
|
||||
str = str.Replace(".", "");
|
||||
}
|
||||
|
||||
return str + "%";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 汉字转拼音缩写
|
||||
|
||||
/// <summary>
|
||||
/// 汉字转拼音缩写
|
||||
/// <returns>拼音缩写</returns>
|
||||
public static string GetPYString(this string str)
|
||||
{
|
||||
string tempStr = "";
|
||||
foreach (char c in str)
|
||||
{
|
||||
if ((int) c >= 33 && (int) c <= 126)
|
||||
{
|
||||
//字母和符号原样保留
|
||||
|
||||
tempStr += c.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
//累加拼音声母
|
||||
tempStr += PinyinHelper.GetPinyin(c).ToCharArray()[0];
|
||||
}
|
||||
}
|
||||
|
||||
return tempStr;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 清楚字符串中所有空格,包括\r\n\t
|
||||
|
||||
/// <summary>
|
||||
/// 清楚字符串中所有空格,包括\r\n\t
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string CleanSpace(this string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
return string.Empty;
|
||||
return Regex.Replace(str, @"\s", "");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 反转义
|
||||
|
||||
/// <summary>
|
||||
/// 反转义
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string Unescape(this string str)
|
||||
{
|
||||
return System.Text.RegularExpressions.Regex.Unescape(str);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 转换成数字
|
||||
|
||||
public static int ToInt(this string str)
|
||||
{
|
||||
int i = 0;
|
||||
int.TryParse(str, out i);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static decimal ToDecimal(this string str)
|
||||
{
|
||||
decimal i = 0;
|
||||
decimal.TryParse(str, out i);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static int ToInt(this string str, int defaultnum = 0)
|
||||
{
|
||||
int i;
|
||||
bool flag = Int32.TryParse(str, out i);
|
||||
if (flag)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
else
|
||||
{
|
||||
return defaultnum;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转为double类型
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToDouble(this string str)
|
||||
{
|
||||
double result = 0;
|
||||
double.TryParse(str, out result);
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 返回字符串真实长度, 1个汉字长度为2
|
||||
|
||||
/// <summary>
|
||||
/// 返回字符串真实长度, 1个汉字长度为2
|
||||
/// </summary>
|
||||
/// <returns>字符长度</returns>
|
||||
public static int GetStringLength(this string str)
|
||||
{
|
||||
return Encoding.Default.GetBytes(str).Length;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 从字符串的指定位置截取指定长度的子字符串
|
||||
|
||||
/// <summary>
|
||||
/// 从字符串的指定位置截取指定长度的子字符串
|
||||
/// </summary>
|
||||
/// <param name="str">原字符串</param>
|
||||
/// <param name="startIndex">子字符串的起始位置</param>
|
||||
/// <param name="length">子字符串的长度</param>
|
||||
/// <returns>子字符串</returns>
|
||||
public static string CutString(this string str, int startIndex, int length)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
if (startIndex >= 0)
|
||||
{
|
||||
if (length < 0)
|
||||
{
|
||||
length = length * -1;
|
||||
if (startIndex - length < 0)
|
||||
{
|
||||
length = startIndex;
|
||||
startIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
startIndex = startIndex - length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (startIndex > str.Length)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (length < 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (length + startIndex > 0)
|
||||
{
|
||||
length = length + startIndex;
|
||||
startIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (str.Length - startIndex < length)
|
||||
{
|
||||
length = str.Length - startIndex;
|
||||
}
|
||||
|
||||
return str.Substring(startIndex, length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 判断文件名是否为浏览器可以直接显示的图片文件名
|
||||
|
||||
/// <summary>
|
||||
/// 判断文件名是否为浏览器可以直接显示的图片文件名
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <returns>是否可以直接显示</returns>
|
||||
public static bool IsImgFilename(this 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");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 取指定长度的字符串
|
||||
|
||||
public static string GetUnicodeSubString(this string str, int len, string p_TailString)
|
||||
{
|
||||
string result = string.Empty; // 最终返回的结果
|
||||
int byteLen = System.Text.Encoding.Default.GetByteCount(str); // 单字节字符长度
|
||||
int charLen = str.Length; // 把字符平等对待时的字符串长度
|
||||
int byteCount = 0; // 记录读取进度
|
||||
int pos = 0; // 记录截取位置
|
||||
if (byteLen > len)
|
||||
{
|
||||
for (int i = 0; i < charLen; i++)
|
||||
{
|
||||
if (Convert.ToInt32(str.ToCharArray()[i]) > 255) // 按中文字符计算加2
|
||||
byteCount += 2;
|
||||
else // 按英文字符计算加1
|
||||
byteCount += 1;
|
||||
if (byteCount > len) // 超出时只记下上一个有效位置
|
||||
{
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
else if (byteCount == len) // 记下当前位置
|
||||
{
|
||||
pos = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (pos >= 0)
|
||||
result = str.Substring(0, pos) + p_TailString;
|
||||
}
|
||||
else
|
||||
result = str;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取指定长度的字符串
|
||||
/// </summary>
|
||||
/// <param name="p_SrcString">要检查的字符串</param>
|
||||
/// <param name="p_StartIndex">起始位置</param>
|
||||
/// <param name="p_Length">指定长度</param>
|
||||
/// <param name="p_TailString">用于替换的字符串</param>
|
||||
/// <returns>截取后的字符串</returns>
|
||||
public static string GetSubString(this string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
|
||||
{
|
||||
string myResult = p_SrcString;
|
||||
|
||||
Byte[] bComments = Encoding.UTF8.GetBytes(p_SrcString);
|
||||
foreach (char c in Encoding.UTF8.GetChars(bComments))
|
||||
{
|
||||
//当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
|
||||
if ((c > '\u0800' && c < '\u4e00') || (c > '\xAC00' && c < '\xD7A3'))
|
||||
{
|
||||
//if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") || System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))
|
||||
//当截取的起始位置超出字段串长度时
|
||||
if (p_StartIndex >= p_SrcString.Length)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
{
|
||||
return p_SrcString.Substring(p_StartIndex,
|
||||
((p_Length + p_StartIndex) > p_SrcString.Length)
|
||||
? (p_SrcString.Length - p_StartIndex)
|
||||
: p_Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (p_Length >= 0)
|
||||
{
|
||||
byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);
|
||||
|
||||
//当字符串长度大于起始位置
|
||||
if (bsSrcString.Length > p_StartIndex)
|
||||
{
|
||||
int p_EndIndex = bsSrcString.Length;
|
||||
|
||||
//当要截取的长度在字符串的有效长度范围内
|
||||
if (bsSrcString.Length > (p_StartIndex + p_Length))
|
||||
{
|
||||
p_EndIndex = p_Length + p_StartIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
//当不在有效范围内时,只取到字符串的结尾
|
||||
|
||||
p_Length = bsSrcString.Length - p_StartIndex;
|
||||
p_TailString = "";
|
||||
}
|
||||
|
||||
|
||||
int nRealLength = p_Length;
|
||||
int[] anResultFlag = new int[p_Length];
|
||||
byte[] bsResult = null;
|
||||
|
||||
int nFlag = 0;
|
||||
for (int i = p_StartIndex; i < p_EndIndex; i++)
|
||||
{
|
||||
if (bsSrcString[i] > 127)
|
||||
{
|
||||
nFlag++;
|
||||
if (nFlag == 3)
|
||||
{
|
||||
nFlag = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
nFlag = 0;
|
||||
}
|
||||
|
||||
anResultFlag[i] = nFlag;
|
||||
}
|
||||
|
||||
if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
|
||||
{
|
||||
nRealLength = p_Length + 1;
|
||||
}
|
||||
|
||||
bsResult = new byte[nRealLength];
|
||||
|
||||
Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);
|
||||
|
||||
myResult = Encoding.Default.GetString(bsResult);
|
||||
|
||||
myResult = myResult + p_TailString;
|
||||
}
|
||||
}
|
||||
|
||||
return myResult;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 自定义的替换字符串函数
|
||||
|
||||
/// <summary>
|
||||
/// 自定义的替换字符串函数
|
||||
/// </summary>
|
||||
public static string ReplaceString(string SourceString, string SearchString, string ReplaceString,
|
||||
bool IsCaseInsensetive)
|
||||
{
|
||||
return Regex.Replace(SourceString, Regex.Escape(SearchString), ReplaceString,
|
||||
IsCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 检测是否符合email格式
|
||||
|
||||
/// <summary>
|
||||
/// 检测是否符合email格式
|
||||
/// </summary>
|
||||
/// <param name="strEmail">要判断的email字符串</param>
|
||||
/// <returns>判断结果</returns>
|
||||
public static bool IsValidEmail(this string strEmail)
|
||||
{
|
||||
return Regex.IsMatch(strEmail,
|
||||
@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 检测是否是正确的Url
|
||||
|
||||
/// <summary>
|
||||
/// 检测是否是正确的Url
|
||||
/// </summary>
|
||||
/// <param name="strUrl">要验证的Url</param>
|
||||
/// <returns>判断结果</returns>
|
||||
public static bool IsURL(this string strUrl)
|
||||
{
|
||||
return Regex.IsMatch(strUrl,
|
||||
@"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 是否为ip
|
||||
|
||||
/// <summary>
|
||||
/// 是否为ip
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIP(this 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?)$");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 是否为手机号
|
||||
|
||||
/// <summary>
|
||||
/// 是否为手机号
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsMobilePhone(this string str)
|
||||
{
|
||||
return Regex.IsMatch(str, @"^0?(13[0-9]|15[0-9]|166|17[0-9]|18[0-9]|19[8-9]|14[5-7])[0-9]{8}$");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 是否为身份证号
|
||||
|
||||
/// <summary>
|
||||
/// 是否为身份证号
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIdCard(this string str)
|
||||
{
|
||||
return Regex.IsMatch(str, @"(^\d{15}$)|(^\d{17}([0-9]|X)$)");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 是否为小数
|
||||
|
||||
/// <summary>
|
||||
/// 是否为小数
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsPositiveNumber(this string str)
|
||||
{
|
||||
return Regex.IsMatch(str, @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 是否为数字
|
||||
|
||||
/// <summary>
|
||||
/// 是否为数字
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNumber(this string str)
|
||||
{
|
||||
return Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 字符串转化为泛型集合
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转化为泛型集合
|
||||
/// </summary>
|
||||
/// <param name="str">字符串</param>
|
||||
/// <param name="splitstr">要分割的字符</param>
|
||||
/// <returns></returns>
|
||||
public static List<T> StrToList<T>(this string str, char splitstr)
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
if (string.IsNullOrEmpty(str))
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
string[] strarray = str.Split(new Char[] {splitstr}, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (string s in strarray)
|
||||
{
|
||||
if (s != "")
|
||||
list.Add((T) Convert.ChangeType(s, typeof(T)));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转化为泛型集合
|
||||
/// </summary>
|
||||
/// <param name="str">字符串</param>
|
||||
/// <returns></returns>
|
||||
public static List<T> StrToList<T>(this string str)
|
||||
{
|
||||
return StrToList<T>(str, ',');
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public static string ToGBK(this string str)
|
||||
{
|
||||
return Encoding.GetEncoding("GBK").GetString(Encoding.Default.GetBytes(str));
|
||||
}
|
||||
|
||||
public static string FromGBK(this string str)
|
||||
{
|
||||
return Encoding.Default.GetString(Encoding.GetEncoding("GBK").GetBytes(str));
|
||||
}
|
||||
|
||||
public static string ToBase64String(this string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(value);
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
public static string FromBase64String(this string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
byte[] bytes = Convert.FromBase64String(value);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
|
||||
public static string RegexMatch(this string str, string pattern)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Regex.Match(str, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Value;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static List<string> RegexMatches(this string str, string pattern)
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
||||
|
||||
if (regex.IsMatch(str))
|
||||
{
|
||||
MatchCollection matchCollection = regex.Matches(str);
|
||||
|
||||
foreach (Match match in matchCollection)
|
||||
{
|
||||
list.Add(match.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Has(this string str)
|
||||
{
|
||||
return !string.IsNullOrEmpty(str?.Trim());
|
||||
}
|
||||
public static bool NotHas(this string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str?.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Nelibur.ObjectMapper;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Hncore.Infrastructure.Extension
|
||||
{
|
||||
public static class TinyMapperExtension
|
||||
{
|
||||
public static void Binds<T1, T2>()
|
||||
{
|
||||
TinyMapper.Bind<T1, T2>();
|
||||
TinyMapper.Bind<T2, T1>();
|
||||
|
||||
TinyMapper.Bind<List<T1>, List<T2>>();
|
||||
TinyMapper.Bind<List<T2>, List<T1>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user