You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.3 KiB
142 lines
4.3 KiB
using Newtonsoft.Json; |
|
using System.Runtime.CompilerServices; |
|
using System.Text; |
|
|
|
namespace WebApplication1 |
|
{ |
|
|
|
public static class ConvertorExtension |
|
{ |
|
/// <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; |
|
} |
|
|
|
} |
|
}
|
|
|