Skip to content

Commit

Permalink
Merge pull request #552 from stormaref/master
Browse files Browse the repository at this point in the history
Fix test bug
  • Loading branch information
andrerav authored Feb 26, 2023
2 parents 0e31a0b + ae0f376 commit 5c82daa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/Mapster.Tests/WhenUsingIMapFrom.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Mapster.Utils;
using MapsterMapper;
Expand All @@ -14,7 +16,13 @@ public class WhenUsingIMapFrom
public WhenUsingIMapFrom()
{
_mapper = new Mapper();
TypeAdapterConfig.GlobalSettings.ScanInheritedTypes(Assembly.GetExecutingAssembly());
var types = new List<Type>
{
typeof(SourceModel),
typeof(InheritedDestinationModel),
typeof(DestinationModel)
};
TypeAdapterConfig.GlobalSettings.ScanInheritedTypes(types);
}

[TestMethod]
Expand Down
14 changes: 5 additions & 9 deletions src/Mapster/Utils/InterfaceDynamicMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

Expand All @@ -7,22 +8,17 @@ namespace Mapster.Utils;
public class InterfaceDynamicMapper
{
private readonly TypeAdapterConfig _config;
private readonly Assembly _assembly;
private readonly List<Type> _types;

public InterfaceDynamicMapper(TypeAdapterConfig config, Assembly assembly)
public InterfaceDynamicMapper(TypeAdapterConfig config, List<Type> types)
{
_config = config;
_assembly = assembly;
_types = types;
}

internal void ApplyMappingFromAssembly()
{
var types = _assembly.GetTypes()
.Where(t =>
t.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)));

foreach (var type in types)
foreach (var type in _types)
{
var instance = Activator.CreateInstance(type);
var method = GetMethod(type);
Expand Down
20 changes: 19 additions & 1 deletion src/Mapster/Utils/TypeAdapterConfigExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Mapster.Utils;
Expand All @@ -6,7 +9,22 @@ public static class TypeAdapterConfigExtensions
{
public static void ScanInheritedTypes(this TypeAdapterConfig config, Assembly assembly)
{
InterfaceDynamicMapper dynamicMapper = new(config, assembly);
var types = assembly.GetTypes()
.Where(t =>
t.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)))
.ToList();
InterfaceDynamicMapper dynamicMapper = new(config, types);
dynamicMapper.ApplyMappingFromAssembly();
}

internal static void ScanInheritedTypes(this TypeAdapterConfig config, List<Type> types)
{
types = types.Where(t =>
t.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)))
.ToList();
InterfaceDynamicMapper dynamicMapper = new(config, types);
dynamicMapper.ApplyMappingFromAssembly();
}
}

0 comments on commit 5c82daa

Please sign in to comment.