AutoMapper可以可以代替json序列化与反序列化的方法进行对象之间的转换。
public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action<IMapperConfigurationExpression> configure = null)
{
List<(Type from, Type[] targets)> maps = new List<(Type from, Type[] targets)>();
maps.AddRange(GlobalData.AllFxTypes.Where(x => x.GetCustomAttribute<MapAttribute>() != null)
.Select(x => (x, x.GetCustomAttribute<MapAttribute>().TargetTypes)));
var configuration = new MapperConfiguration(cfg =>
{
maps.ForEach(aMap =>
{
aMap.targets.ToList().ForEach(aTarget =>
{
cfg.CreateMap(aMap.from, aTarget).IgnoreAllNonExisting(aMap.from, aTarget).ReverseMap();
});
});
cfg.AddMaps(GlobalData.AllFxAssemblies);
//自定义映射
configure?.Invoke(cfg);
});