忽略
This commit is contained in:
@@ -1,21 +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());
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,177 +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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,149 +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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,25 +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;
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,94 +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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,126 +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
|
||||
|
||||
}
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,19 +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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,63 +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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,144 +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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,46 +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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,17 +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>>();
|
||||
}
|
||||
}
|
||||
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