2020-03-24 13:01:14 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Application.Common.Mappings
|
|
|
|
|
{
|
|
|
|
|
public class MappingProfile : Profile
|
|
|
|
|
{
|
|
|
|
|
public MappingProfile()
|
|
|
|
|
{
|
2020-03-24 17:31:34 +01:00
|
|
|
|
ApplyMappingsFromAssembly(typeof(MappingProfile).GetTypeInfo().Assembly);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplyMappingsFromAssembly(Assembly assembly)
|
|
|
|
|
{
|
2020-03-24 17:31:34 +01:00
|
|
|
|
var types = assembly.ExportedTypes
|
|
|
|
|
.Where(t => t.GetTypeInfo().ImplementedInterfaces.Any(i =>
|
|
|
|
|
i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)))
|
2020-03-24 13:01:14 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
{
|
|
|
|
|
var instance = Activator.CreateInstance(type);
|
2020-03-24 17:31:34 +01:00
|
|
|
|
var methodInfo = type.GetTypeInfo().GetDeclaredMethod("Mapping");
|
2020-03-24 13:01:14 +01:00
|
|
|
|
methodInfo?.Invoke(instance, new object[] { this });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|