|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Text;
|
|
|
|
using System;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace Elight.Utility.Extensions
|
|
|
|
{
|
|
|
|
public static class ConvertorHelper
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// model=>json string
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="t"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
private readonly static JsonSerializerSettings settings = new JsonSerializerSettings()
|
|
|
|
{
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
|
|
};
|
|
|
|
public static string ConvertToJsonStr<T>(this T t)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return JsonConvert.SerializeObject(t, Formatting.None, settings);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static T DeepCopy<T>(this T t)
|
|
|
|
{
|
|
|
|
return t.ConvertToJsonStr().ConvertToModel<T>();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// json转为匿名对象
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="json"></param>
|
|
|
|
/// <param name="anonymousTypeObject"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static T ConvertToAnonymousType<T>(this object json, T anonymousTypeObject)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return JsonConvert.DeserializeAnonymousType(json.ToString(), anonymousTypeObject);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// object 转匿名类
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="anonymous"></param>
|
|
|
|
/// <param name="anonymousType"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static T ConvertToAnonymous<T>(this object anonymous, T anonymousType)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (anonymous != null)
|
|
|
|
{
|
|
|
|
return (T)anonymous;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return default;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// json string=>mdoel
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="str"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static T ConvertToModel<T>(this string str, [CallerMemberName] string methodname = "")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return JsonConvert.DeserializeObject<T>(str);
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static string ConvertToBase64(this string str)
|
|
|
|
{
|
|
|
|
return Convert.ToBase64String(Encoding.Default.GetBytes(str));
|
|
|
|
}
|
|
|
|
public static string ConvertToGetParam(this object obj)
|
|
|
|
{
|
|
|
|
StringBuilder strBui = new StringBuilder();
|
|
|
|
|
|
|
|
System.Reflection.PropertyInfo[] proArray = obj.GetType().GetProperties();
|
|
|
|
foreach (System.Reflection.PropertyInfo pro in proArray)
|
|
|
|
{
|
|
|
|
if (strBui.Length < 1)
|
|
|
|
{
|
|
|
|
strBui.Append("?");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strBui.Append("&");
|
|
|
|
}
|
|
|
|
strBui.Append(string.Format("{0}={1}", pro.Name, pro.GetValue(obj, null)));
|
|
|
|
}
|
|
|
|
return strBui.ToString();
|
|
|
|
}
|
|
|
|
// DateTime --> long
|
|
|
|
public static long ConvertDateTimeToLong(DateTime dt)
|
|
|
|
{
|
|
|
|
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|
|
|
TimeSpan toNow = dt.Subtract(dtStart);
|
|
|
|
long timeStamp = toNow.Ticks;
|
|
|
|
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
|
|
|
|
return timeStamp;
|
|
|
|
}
|
|
|
|
// long --> DateTime
|
|
|
|
public static DateTime ConvertLongToDateTime(long d)
|
|
|
|
{
|
|
|
|
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|
|
|
long lTime = long.Parse(d + "0000");
|
|
|
|
TimeSpan toNow = new TimeSpan(lTime);
|
|
|
|
DateTime dtResult = dtStart.Add(toNow);
|
|
|
|
return dtResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class DateHelper
|
|
|
|
{
|
|
|
|
public static DateTime GetStartDateOfDay(DateTime date)
|
|
|
|
{
|
|
|
|
return date.Date;
|
|
|
|
}
|
|
|
|
public static DateTime GetEndDateOfDay(DateTime date)
|
|
|
|
{
|
|
|
|
return date.Date.AddDays(1).AddSeconds(-1);
|
|
|
|
}
|
|
|
|
public static DateTime GetStartDateOfMonth(DateTime date)
|
|
|
|
{
|
|
|
|
return date.AddDays(1 - date.Day).Date;
|
|
|
|
}
|
|
|
|
public static DateTime GetEndDateOfMonth(DateTime date)
|
|
|
|
{
|
|
|
|
return GetStartDateOfMonth(date).AddMonths(1).AddSeconds(-1);
|
|
|
|
}
|
|
|
|
public static DateTime GetStartDateOfYear(DateTime date)
|
|
|
|
{
|
|
|
|
return date.AddDays(1 - date.Day).AddMonths(1 - date.Month).Date;
|
|
|
|
}
|
|
|
|
public static DateTime GetEndDateOfYear(DateTime date)
|
|
|
|
{
|
|
|
|
return GetStartDateOfYear(date).AddYears(1).AddSeconds(-1);
|
|
|
|
}
|
|
|
|
public static List<DateTime> GetArrayByDay(DateTime starttime, DateTime endtime)
|
|
|
|
{
|
|
|
|
var list = new List<DateTime>();
|
|
|
|
for (DateTime dt = starttime; dt.Date <= endtime.Date; dt = dt.AddDays(1))
|
|
|
|
{
|
|
|
|
list.Add(dt.Date);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
public static List<DateTime> GetArrayByMonth(DateTime starttime, DateTime endtime)
|
|
|
|
{
|
|
|
|
var list = new List<DateTime>();
|
|
|
|
for (DateTime dt = starttime; dt.Date <= endtime.Date; dt = dt.AddMonths(1))
|
|
|
|
{
|
|
|
|
list.Add(GetStartDateOfMonth(dt.Date));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
public static List<DateTime> GetArrayByYear(DateTime starttime, DateTime endtime)
|
|
|
|
{
|
|
|
|
var list = new List<DateTime>();
|
|
|
|
for (DateTime dt = starttime; dt.Date <= endtime.Date; dt = dt.AddYears(1))
|
|
|
|
{
|
|
|
|
list.Add(GetStartDateOfYear(dt.Date));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|