From e05d105247cbca8e9fa4fd9292fce7ef80e33a54 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 16 Aug 2023 09:19:48 +0800 Subject: [PATCH] Update DefaultObjectMapper.cs --- .../Abp/ObjectMapping/DefaultObjectMapper.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs b/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs index 9751a4a063..17c821f3ce 100644 --- a/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs +++ b/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs @@ -7,7 +7,6 @@ using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using Volo.Abp.DependencyInjection; -using Volo.Abp.Reflection; namespace Volo.Abp.ObjectMapping; @@ -134,21 +133,27 @@ public class DefaultObjectMapper : IObjectMapper, ITransientDependency var sourceGenericTypeDefinition = typeof(TSource).GenericTypeArguments[0]; var destinationGenericTypeDefinition = typeof(TDestination).GenericTypeArguments[0]; - var specificGenericTypeDefinitionMapper = serviceScope.ServiceProvider.GetService(typeof(IObjectMapper<,>).MakeGenericType(sourceGenericTypeDefinition, destinationGenericTypeDefinition)); - if (specificGenericTypeDefinitionMapper == null) + var mapperType = typeof(IObjectMapper<,>).MakeGenericType(sourceGenericTypeDefinition, destinationGenericTypeDefinition); + var specificMapper = serviceScope.ServiceProvider.GetService(mapperType); + if (specificMapper == null) { //skip, no specific mapper return default; } - var cacheKey = $"{specificGenericTypeDefinitionMapper.GetType().FullName}-{(destination == null ? "MapMethodWithSingleParameter" : "MapMethodWithDoubleParameters")}"; - var method = MethodInfoCache.GetOrAdd(cacheKey, x => specificGenericTypeDefinitionMapper.GetType().GetMethods().First(m => m.Name == nameof(IObjectMapper.Map) && m.GetParameters().Length == (destination == null ? 1 : 2))); + var cacheKey = $"{mapperType.FullName}{(destination == null ? "MapMethodWithSingleParameter" : "MapMethodWithDoubleParameters")}"; + var method = MethodInfoCache.GetOrAdd(cacheKey, _ => + { + return specificMapper.GetType().GetMethods().First(x => x.Name == nameof(IObjectMapper.Map) && + x.GetParameters().Length == (destination == null ? 1 : 2)); + }); + var result = Activator.CreateInstance(typeof(Collection<>).MakeGenericType(destinationGenericTypeDefinition))!.As(); foreach (var sourceItem in (IEnumerable)source!) { result.Add(destination == null - ? method.Invoke(specificGenericTypeDefinitionMapper, new [] {sourceItem})! - : method.Invoke(specificGenericTypeDefinitionMapper, new [] {sourceItem, Activator.CreateInstance(destinationGenericTypeDefinition)!})!); + ? method.Invoke(specificMapper, new [] { sourceItem })! + : method.Invoke(specificMapper, new [] { sourceItem, Activator.CreateInstance(destinationGenericTypeDefinition)! })!); } return (TDestination)result!;