为Object新增toJson扩展方法

This commit is contained in:
falcon 2023-05-05 13:54:27 +08:00
parent e442a099e6
commit 4db845412d

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json;
namespace Falcon.SugarApi namespace Falcon.SugarApi
{ {
@ -19,8 +20,8 @@ namespace Falcon.SugarApi
/// <param name="source">原对象</param> /// <param name="source">原对象</param>
/// <param name="target">目标对象</param> /// <param name="target">目标对象</param>
public static TSource CloneTo<TSource>(this TSource source,[NotNull] object target) where TSource : class { public static TSource CloneTo<TSource>(this TSource source,[NotNull] object target) where TSource : class {
_=source??throw new ArgumentNullException(nameof(source)); _ = source ?? throw new ArgumentNullException(nameof(source));
_=target??throw new ArgumentNullException(nameof(target)); _ = target ?? throw new ArgumentNullException(nameof(target));
var all = from s in source.GetType().GetProperties() var all = from s in source.GetType().GetProperties()
join t in target.GetType().GetProperties() on s.Name equals t.Name join t in target.GetType().GetProperties() on s.Name equals t.Name
select new { s,t }; select new { s,t };
@ -50,15 +51,15 @@ namespace Falcon.SugarApi
/// <param name="targetType">目标类型</param> /// <param name="targetType">目标类型</param>
/// <returns>转换后的类型</returns> /// <returns>转换后的类型</returns>
public static object? ChangeType(this object? source,Type targetType) { public static object? ChangeType(this object? source,Type targetType) {
if(targetType==null) { if(targetType == null) {
throw new ArgumentNullException("targetType"); throw new ArgumentNullException("targetType");
} }
if(source==null) { if(source == null) {
return null; return null;
} }
if(targetType.IsGenericType&&targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { if(targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
NullableConverter nullableConverter = new NullableConverter(targetType); NullableConverter nullableConverter = new NullableConverter(targetType);
targetType=nullableConverter.UnderlyingType; targetType = nullableConverter.UnderlyingType;
} }
return Convert.ChangeType(source,targetType); return Convert.ChangeType(source,targetType);
} }
@ -70,7 +71,7 @@ namespace Falcon.SugarApi
/// <returns>对象本身</returns> /// <returns>对象本身</returns>
/// <exception cref="ArgumentNullException">对象为null</exception> /// <exception cref="ArgumentNullException">对象为null</exception>
public static object? ThrowNullExceptionWhenNull(this object? obj) { public static object? ThrowNullExceptionWhenNull(this object? obj) {
if(obj==null) { if(obj == null) {
throw new ArgumentNullException(); throw new ArgumentNullException();
} }
return obj; return obj;
@ -84,9 +85,9 @@ namespace Falcon.SugarApi
public static IEnumerable<ExpandPropertyInfo> ExpandProperties(this object obj) { public static IEnumerable<ExpandPropertyInfo> ExpandProperties(this object obj) {
foreach(PropertyInfo p in obj.GetType().GetProperties()) { foreach(PropertyInfo p in obj.GetType().GetProperties()) {
yield return new ExpandPropertyInfo { yield return new ExpandPropertyInfo {
Name=p.Name, Name = p.Name,
Type=p.PropertyType, Type = p.PropertyType,
Value=p.CanRead ? p.GetValue(obj) : null, Value = p.CanRead ? p.GetValue(obj) : null,
}; };
} }
} }
@ -96,7 +97,7 @@ namespace Falcon.SugarApi
/// </summary> /// </summary>
/// <param name="obj">要测试的对象</param> /// <param name="obj">要测试的对象</param>
/// <returns>True表示对象为null否则不为null</returns> /// <returns>True表示对象为null否则不为null</returns>
public static bool IsNull([AllowNull] this object obj) => obj==null; public static bool IsNull([AllowNull] this object obj) => obj == null;
/// <summary> /// <summary>
/// 对象是否不为null。与IsNull相反 /// 对象是否不为null。与IsNull相反
@ -113,7 +114,7 @@ namespace Falcon.SugarApi
/// <exception cref="ObjectSetValueException">设置属性值引发的异常</exception> /// <exception cref="ObjectSetValueException">设置属性值引发的异常</exception>
public static T SetPropertyValue<T>(this T obj,Func<PropertyInfo,object?,object?> getValue) where T : class { public static T SetPropertyValue<T>(this T obj,Func<PropertyInfo,object?,object?> getValue) where T : class {
foreach(PropertyInfo info in obj.GetType().GetProperties()) { foreach(PropertyInfo info in obj.GetType().GetProperties()) {
if(info.CanWrite&&info.CanRead) { if(info.CanWrite && info.CanRead) {
object? originalVal = info.GetValue(obj); object? originalVal = info.GetValue(obj);
var val = getValue(info,originalVal); var val = getValue(info,originalVal);
try { try {
@ -126,6 +127,20 @@ namespace Falcon.SugarApi
} }
return obj; return obj;
} }
/// <summary>
/// 将对象转换为Json字符串格式
/// </summary>
/// <typeparam name="T">对象的类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="OptionBuilder">转换参数</param>
/// <returns>转换后字符串</returns>
public static string ToJson<T>(this T obj,Action<JsonSerializerOptions>? OptionBuilder = null) {
var option = new JsonSerializerOptions();
OptionBuilder?.Invoke(option);
var ser = new JsonSerialize.JsonSerializeFactory().CreateJsonSerialize(option);
return ser.Serialize(obj);
}
} }
/// <summary> /// <summary>
@ -161,9 +176,9 @@ namespace Falcon.SugarApi
/// <param name="innExceprtion">内部异常</param> /// <param name="innExceprtion">内部异常</param>
public ObjectSetValueException(object? obj,PropertyInfo? info,object? val,Exception innExceprtion) public ObjectSetValueException(object? obj,PropertyInfo? info,object? val,Exception innExceprtion)
: base("为目标属性设置值时引发的异常",innExceprtion) { : base("为目标属性设置值时引发的异常",innExceprtion) {
this.Obj=obj; this.Obj = obj;
this.Property=info; this.Property = info;
this.Value=val; this.Value = val;
} }
/// <summary> /// <summary>