123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.Encodings.Web;
- using System.Text.Json;
- using System.Text.Json.Serialization;
- using System.Text.Unicode;
- using System.Threading.Tasks;
- public static class ObjectExtends
- {
- private class DateTimeConverter : JsonConverter<DateTime>
- {
- public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- return DateTime.Parse(reader.GetString());
- }
- public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
- {
- writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
- }
- }
- /// <summary>
- /// 判断obj是否是数字
- /// <para>bool也算数字</para>
- /// <para>null返回false</para>
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static bool IsNumericType(this Type t)
- {
- //常用类型放前面提高效率
- if (t == null) return false;
- if (t == typeof(string) || t == typeof(DateTime)) return false;
- if (t == typeof(int)) return true;
- if (t == typeof(double)) return true;
- if (t == typeof(float)) return true;
- if (t == typeof(long)) return true;
- if (t == typeof(bool)) return true;
- if (t == typeof(byte)) return true;
- if (t == typeof(sbyte)) return true;
- if (t == typeof(ushort)) return true;
- if (t == typeof(uint)) return true;
- if (t == typeof(ulong)) return true;
- if (t == typeof(short)) return true;
- if (t == typeof(decimal)) return true;
- return false;
- }
- /// <summary>
- /// 判断obj是否是非整数类型
- /// <para>包含float、double、decimal</para>
- /// <para>null返回false</para>
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static bool IsFloatType(this Type t)
- {
- if (t == null) return false;
- if (t == typeof(double)) return true;
- if (t == typeof(float)) return true;
- if (t == typeof(decimal)) return true;
- return false;
- }
- /// <summary>
- /// <para>将源对象的属性值映射到target对象上,(target==null时直接返回)</para>
- /// <para>该方法使用反射</para>
- /// <para>源对象和目标对象可以是不同对象,只要属性名称一致即可,如Model1映射到Model2</para>
- /// </summary>
- /// <typeparam name="T">target泛型类</typeparam>
- /// <param name="obj">源对象</param>
- /// <param name="target">目标对象</param>
- public static void MapTo<T>(this object obj, T target)
- {
- if (obj == null || target == null) return;
- var objProps = obj.GetType().GetProperties();
- if (objProps == null || objProps.Length == 0) return;
- var listObjProps = objProps.ToList();
- //var str = JsonConvert.SerializeObject(obj);
- //var foo = JsonConvert.DeserializeObject<T>(str);
- var props = typeof(T).GetProperties();
- foreach (var item in props)
- {
- if (!item.CanWrite) continue;
- var find = listObjProps.Find(p => p.Name == item.Name);
- if (find == null) continue;
- var value = find.GetValue(obj);
- if (value == null) continue;
- ;
- if (item.PropertyType != value.GetType() && !item.PropertyType.Name.Contains("Nullable"))
- {
- value = Convert.ChangeType(value, item.PropertyType);
- }
- item.SetValue(target, value);
- }
- }
- /// <summary>
- /// <para>将源对象的映射为一个新对象</para>
- /// <para>该方法使用json序列化,对象嵌套深度不能超过8层</para>
- /// <para>源对象和目标对象可以是不同对象,只要属性名称一致即可,如Model1映射到Model2</para>
- /// </summary>
- /// <typeparam name="T">返回对象泛型类型</typeparam>
- /// <param name="obj">源对象</param>
- /// <returns></returns>
- public static T MapTo<T>(this object obj)
- {
- string str;
- JsonSerializerOptions options = null;
- if (obj == null)
- str = "";
- else if (obj.GetType() == typeof(string))
- str = obj.ToString();
- else if (obj.GetType() == typeof(StringBuilder))
- str = ((StringBuilder)obj).ToString();
- else
- {
- options = new JsonSerializerOptions();
- options.WriteIndented = false;
- options.IncludeFields = true;
- options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
- options.Converters.Add(new DateTimeConverter());//yyyy-MM-dd HH:mm:ss
- options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
- str = JsonSerializer.Serialize(obj, options);
- }
- if (options == null)
- {
- options = new JsonSerializerOptions();
- options.WriteIndented = false;
- options.IncludeFields = true;
- options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
- options.Converters.Add(new DateTimeConverter());//yyyy-MM-dd HH:mm:ss
- options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
- }
- if (string.IsNullOrWhiteSpace(str)) return default;
- var res = JsonSerializer.Deserialize<T>(str, options);
- return res;
- }
- /// <summary>
- /// <para>对象转JsonString</para>
- /// <para>如果对象本身是String类型则直接返回</para>
- /// </summary>
- /// <param name="obj">对象</param>
- /// <param name="formarting">是否开启格式化,格式化会多占用一些字节,默认不格式化</param>
- /// <returns></returns>
- public static string ToJsonStr(this object obj, bool formarting = false)
- {
- if (obj == null) return "";
- if (obj.GetType() == typeof(string)) return obj as string;
- if (obj.GetType() == typeof(StringBuilder)) return ((StringBuilder)obj).ToString();
- var options = new JsonSerializerOptions();
- options.WriteIndented = formarting;
- options.IncludeFields = true;
- options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
- options.Converters.Add(new DateTimeConverter());//yyyy-MM-dd HH:mm:ss
- //不加这句非ASCII字符会被序列化为Unicode编号,类似于\u8C03
- options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
- var res = JsonSerializer.Serialize(obj, options);
- return res;
- }
- }
|