如何使用Refection(以通用方式)在映射器配置中添加现有配置文件

问题描述 投票:0回答:1

我有一个单独的类,用于在源和目标之间进行映射。例如

注册映射:

public class RegistrationMapping
{
    public static void Map(IProfileExpression profile)
    {
       profile.CreateMap<DB_Registration, Registration>()
            .ForMember(x => x.EMP_ID, map => map.MapFrom(c => c.employeeID))
            .ForMember(x => x.MOB_NO, map => map.MapFrom(c => c.Mobile))
            .ForMember(x => x.EMAIL_ID, map => map.MapFrom(c => c.EmailID))
     }
}

[以类似的方式,我也有用于其他映射的类。

现在在我的存储库中,我想这样使用,

// I want to achieve below code in a generic way. 
var config = new MapperConfiguration(cfg => cfg.AddProfile(/*RegistrationMapping goes here*/)); 
var mappedConfigurations = config.GetAllTypeMaps(); // This line of code is needed for my other purpose(get unmapped properties) 

任何帮助将不胜感激。

c# .net entity-framework reflection automapper
1个回答
0
投票

AutoMapper允许通过传递程序集,程序集名称或程序集中包含的类型来加载配置文件。

您的RegistrationMapping类和其他类必须从AutoMapper.Profile继承。

您可以这样加载个人资料:

按名称:

 Mapper.Initialize(x => x.AddProfiles("MyApplication.RegistrationMapping"));

按类型:

 Mapper.Initialize(x => x.AddProfiles(typeof(RegistrationMapping))); 

通过组装:

 Mapper.Initialize(x => x.AddProfiles(typeof(RegistrationMapping).Assembly));

 Mapper.Initialize(x => x.AddProfiles(Assembly.GetExecutingAssembly()));
© www.soinside.com 2019 - 2024. All rights reserved.