1111
1212 public static class ServiceCollectionExtensions
1313 {
14- private static readonly Assembly AutoMapperAssembly = typeof ( Mapper ) . GetTypeInfo ( ) . Assembly ;
15-
1614 private static readonly Action < IMapperConfigurationExpression > DefaultConfig = cfg => { } ;
1715#if DEPENDENCY_MODEL
1816
1917 private static HashSet < string > ReferenceAssemblies { get ; } = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
2018 {
21- //"Microsoft.AspNetCore.Mvc",
22- //"Microsoft.AspNetCore.Mvc.Abstractions",
23- //"Microsoft.AspNetCore.Mvc.ApiExplorer",
24- //"Microsoft.AspNetCore.Mvc.Core",
25- //"Microsoft.AspNetCore.Mvc.Cors",
26- //"Microsoft.AspNetCore.Mvc.DataAnnotations",
27- //"Microsoft.AspNetCore.Mvc.Formatters.Json",
28- //"Microsoft.AspNetCore.Mvc.Formatters.Xml",
29- //"Microsoft.AspNetCore.Mvc.Localization",
30- //"Microsoft.AspNetCore.Mvc.Razor",
31- //"Microsoft.AspNetCore.Mvc.Razor.Host",
32- //"Microsoft.AspNetCore.Mvc.TagHelpers",
33- //"Microsoft.AspNetCore.Mvc.ViewFeatures",
3419 "AutoMapper"
3520 } ;
3621
@@ -201,18 +186,21 @@ private static IServiceCollection AddAutoMapperClasses(IServiceCollection servic
201186 additionalInitAction = additionalInitAction ?? DefaultConfig ;
202187 assembliesToScan = assembliesToScan as Assembly [ ] ?? assembliesToScan . ToArray ( ) ;
203188
204- var allTypes = assembliesToScan . SelectMany ( a => a . ExportedTypes ) . ToArray ( ) ;
189+ var allTypes = assembliesToScan
190+ . Where ( a => a . GetName ( ) . Name != nameof ( AutoMapper ) )
191+ . SelectMany ( a => a . DefinedTypes )
192+ . ToArray ( ) ;
205193
206194 var profiles =
207195 allTypes
208- . Where ( t => typeof ( Profile ) . GetTypeInfo ( ) . IsAssignableFrom ( t . GetTypeInfo ( ) ) )
209- . Where ( t => ! t . GetTypeInfo ( ) . IsAbstract ) ;
196+ . Where ( t => typeof ( Profile ) . GetTypeInfo ( ) . IsAssignableFrom ( t ) )
197+ . Where ( t => ! t . IsAbstract ) ;
210198
211199 Mapper . Initialize ( cfg =>
212200 {
213201 additionalInitAction ( cfg ) ;
214202
215- foreach ( var profile in profiles )
203+ foreach ( var profile in profiles . Select ( t => t . AsType ( ) ) )
216204 {
217205 cfg . AddProfile ( profile ) ;
218206 }
@@ -227,11 +215,11 @@ private static IServiceCollection AddAutoMapperClasses(IServiceCollection servic
227215 foreach ( var openType in openTypes )
228216 {
229217 foreach ( var type in allTypes
230- . Where ( t => t . GetTypeInfo ( ) . IsClass )
231- . Where ( t => ! t . GetTypeInfo ( ) . IsAbstract )
232- . Where ( t => t . ImplementsGenericInterface ( openType ) ) )
218+ . Where ( t => t . IsClass )
219+ . Where ( t => ! t . IsAbstract )
220+ . Where ( t => t . AsType ( ) . ImplementsGenericInterface ( openType ) ) )
233221 {
234- services . AddTransient ( type ) ;
222+ services . AddTransient ( type . AsType ( ) ) ;
235223 }
236224 }
237225
0 commit comments