using AutoMapper; using System; using System.Linq; using System.Reflection; namespace MyWarehouse.Application.Common.Mapping { /// <summary> /// AutoMapper profile configuring automatic mapping for DTOs that implement IMapFrom<> interface. /// This profile is automatically picked up by AutoMapper, provided that AutoMapper is properly registered. /// </summary> public class MappingProfile : Profile { public MappingProfile() { ApplyMappingsFromAssembly(Assembly.GetExecutingAssembly()); } private void ApplyMappingsFromAssembly(Assembly assembly) { var types = assembly.GetExportedTypes() .Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>))) .ToList(); foreach (var type in types) { var instance = Activator.CreateInstance(type); var methodInfo = type.GetMethod("Mapping") ?? type.GetInterface("IMapFrom`1")?.GetMethod("Mapping"); methodInfo?.Invoke(instance, new object[] { this }); } } } }