Files
glz e493c85d08 refactor,feat: 批量代码重构与新增业务模块
1. 重命名枚举项与实体字段,修正类型引用
2. 新增IsNull扩展方法与多项字符串处理扩展
3. 新增大量业务DTO、服务接口与枚举定义
4. 重构RabbitMQ服务实现,替换旧版消息队列组件
5. 优化签到服务的宠物喂养事务逻辑
6. 移除冗余的项目引用与旧版消息队列代码
7. 新增Excel导出、导入模板相关工具方法
2026-06-11 17:01:24 +08:00

105 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
namespace QYZH.InteractiveMagazine.Common.Extensions
{
/// <summary>
/// 对象扩展方法
/// </summary>
public static class ObjectExtension
{
/// <summary>
/// 将源对象的属性值拷贝到目标对象
/// </summary>
/// <typeparam name="T">目标对象类型</typeparam>
/// <param name="source">源对象</param>
/// <param name="target">目标对象</param>
/// <returns>目标对象</returns>
public static T CopyTo<T>(this object source, T target)
{
if (source == null || target == null)
{
return target;
}
Type sourceType = source.GetType();
Type targetType = target.GetType();
PropertyInfo[] sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo sourceProp in sourceProperties)
{
if (!sourceProp.CanRead)
{
continue;
}
PropertyInfo? targetProp = targetType.GetProperty(sourceProp.Name);
if (targetProp != null && targetProp.CanWrite &&
targetProp.PropertyType == sourceProp.PropertyType)
{
object? value = sourceProp.GetValue(source);
targetProp.SetValue(target, value);
}
}
return target;
}
/// <summary>
/// 将对象转换为JSON字符串
/// </summary>
/// <param name="obj">对象</param>
/// <param name="formatting">格式化选项</param>
/// <returns>JSON字符串</returns>
public static string ToJson(this object obj, Formatting formatting = Formatting.None)
{
if (obj == null)
{
return string.Empty;
}
return JsonConvert.SerializeObject(obj, formatting);
}
/// <summary>
/// 将对象转换为字典
/// </summary>
/// <param name="obj">对象</param>
/// <returns>字典</returns>
public static Dictionary<string, object?> ToDictionary(this object obj)
{
if (obj == null)
{
return new Dictionary<string, object?>();
}
var dictionary = new Dictionary<string, object?>();
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
if (property.CanRead)
{
dictionary[property.Name] = property.GetValue(obj);
}
}
return dictionary;
}
public static bool IsNull<T>(this IList<T>? s)
{
return s == null || s?.Count() < 1;
}
public static bool IsNull(this object? s)
{
return s == null;
}
}
}