Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge latest from master to development #553

Merged
merged 3 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}