From 4db845412dd35d58354ce95b49a12c8b540d5a57 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Fri, 5 May 2023 13:54:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAObject=E6=96=B0=E5=A2=9EtoJson?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/ObjectExtend.cs | 45 ++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/Falcon.SugarApi/ObjectExtend.cs b/Falcon.SugarApi/ObjectExtend.cs index 197c49d..2a2d0ae 100644 --- a/Falcon.SugarApi/ObjectExtend.cs +++ b/Falcon.SugarApi/ObjectExtend.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; +using System.Text.Json; namespace Falcon.SugarApi { @@ -19,8 +20,8 @@ namespace Falcon.SugarApi /// 原对象 /// 目标对象 public static TSource CloneTo(this TSource source,[NotNull] object target) where TSource : class { - _=source??throw new ArgumentNullException(nameof(source)); - _=target??throw new ArgumentNullException(nameof(target)); + _ = source ?? throw new ArgumentNullException(nameof(source)); + _ = target ?? throw new ArgumentNullException(nameof(target)); var all = from s in source.GetType().GetProperties() join t in target.GetType().GetProperties() on s.Name equals t.Name select new { s,t }; @@ -50,15 +51,15 @@ namespace Falcon.SugarApi /// 目标类型 /// 转换后的类型 public static object? ChangeType(this object? source,Type targetType) { - if(targetType==null) { + if(targetType == null) { throw new ArgumentNullException("targetType"); } - if(source==null) { + if(source == null) { return null; } - if(targetType.IsGenericType&&targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { + if(targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { NullableConverter nullableConverter = new NullableConverter(targetType); - targetType=nullableConverter.UnderlyingType; + targetType = nullableConverter.UnderlyingType; } return Convert.ChangeType(source,targetType); } @@ -70,7 +71,7 @@ namespace Falcon.SugarApi /// 对象本身 /// 对象为null public static object? ThrowNullExceptionWhenNull(this object? obj) { - if(obj==null) { + if(obj == null) { throw new ArgumentNullException(); } return obj; @@ -84,9 +85,9 @@ namespace Falcon.SugarApi public static IEnumerable ExpandProperties(this object obj) { foreach(PropertyInfo p in obj.GetType().GetProperties()) { yield return new ExpandPropertyInfo { - Name=p.Name, - Type=p.PropertyType, - Value=p.CanRead ? p.GetValue(obj) : null, + Name = p.Name, + Type = p.PropertyType, + Value = p.CanRead ? p.GetValue(obj) : null, }; } } @@ -96,7 +97,7 @@ namespace Falcon.SugarApi /// /// 要测试的对象 /// True表示对象为null,否则不为null - public static bool IsNull([AllowNull] this object obj) => obj==null; + public static bool IsNull([AllowNull] this object obj) => obj == null; /// /// 对象是否不为null。与IsNull相反 @@ -113,7 +114,7 @@ namespace Falcon.SugarApi /// 设置属性值引发的异常 public static T SetPropertyValue(this T obj,Func getValue) where T : class { foreach(PropertyInfo info in obj.GetType().GetProperties()) { - if(info.CanWrite&&info.CanRead) { + if(info.CanWrite && info.CanRead) { object? originalVal = info.GetValue(obj); var val = getValue(info,originalVal); try { @@ -126,6 +127,20 @@ namespace Falcon.SugarApi } return obj; } + + /// + /// 将对象转换为Json字符串格式 + /// + /// 对象的类型 + /// 对象 + /// 转换参数 + /// 转换后字符串 + public static string ToJson(this T obj,Action? OptionBuilder = null) { + var option = new JsonSerializerOptions(); + OptionBuilder?.Invoke(option); + var ser = new JsonSerialize.JsonSerializeFactory().CreateJsonSerialize(option); + return ser.Serialize(obj); + } } /// @@ -161,9 +176,9 @@ namespace Falcon.SugarApi /// 内部异常 public ObjectSetValueException(object? obj,PropertyInfo? info,object? val,Exception innExceprtion) : base("为目标属性设置值时引发的异常",innExceprtion) { - this.Obj=obj; - this.Property=info; - this.Value=val; + this.Obj = obj; + this.Property = info; + this.Value = val; } ///