using AspectCore.Extensions.Reflection; using System.Reflection; namespace ZhiYi.Core.Application.Shared { public static class CustomerAppService { /// /// 自定义映射,默认值不映射 /// /// /// /// /// public static void ConditionalMap(TSource source, TDest destination) where TSource : class where TDest : class { var sourceType = typeof(TSource); var destType = typeof(TDest); foreach (var prop in sourceType.GetProperties()) { var destProp = destType.GetProperty(prop.Name); if (destProp != null) { var defaultValue = prop.PropertyType.GetDefaultValue(); var sourceValue = prop.GetValue(source); if (sourceValue != null && !sourceValue.Equals(defaultValue)) { destProp.SetValue(destination, sourceValue); } } } } /// /// 从所有字符串字段中删除前导和尾随空格. /// /// /// public static void TrimStringFields(this T obj) where T : class { if (obj is null) return; var stringProperties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p => p.PropertyType == typeof(string)); foreach (var property in stringProperties) { var propertyValue = (string?)property.GetValue(obj); if (propertyValue != null) { var trimmedValue = propertyValue.Trim(); property.SetValue(obj, trimmedValue); } } } } }