Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit 24a512c

Browse files
committed
Pulling from all types; fixes #9
1 parent f463ab3 commit 24a512c

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

src/AutoMapper.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,11 @@
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

test/AutoMapper.Extensions.Microsoft.DependencyInjection.Tests/Profiles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Profile1()
2929

3030
public abstract class AbstractProfile : Profile { }
3131

32-
public class Profile2 : Profile
32+
internal class Profile2 : Profile
3333
{
3434
public Profile2()
3535
{

0 commit comments

Comments
 (0)