1

在以前版本的 AutoMapper 中,我曾经能够像这样配置 AutoMapper:

public static class AutoMapperFactory
{
    public static IConfigurationProvider CreateMapperConfiguration()
    {
        var config = new MapperConfiguration(cfg =>
        {
            //Scan *.UI assembly for AutoMapper Profiles
            var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));

            cfg.AddProfiles(assembly);

            cfg.IgnoreAllUnmapped();
        });

        return config;
    }
}

现在,这句话cfg.AddProfiles(assembly)给了我错误:Argument 1: cannot convert from 'System.Reflection.Assembly' to 'System.Collections.Generic.IEnumerable<AutoMapper.Profile>

我怎样才能得到一个IEnumerable<AutoMapper.Profile>作为参数传递的AddProfiles

4

1 回答 1

1

您可以使用 addMaps,而不是 addProfile,如下所示:

public static class AutoMapperFactory
{
    public static IConfigurationProvider CreateMapperConfiguration()
    {
        var config = new MapperConfiguration(cfg =>
        {
            //Scan *.UI assembly for AutoMapper Profiles
            var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));

            cfg.AddMaps(assembly);

            cfg.IgnoreAllUnmapped();
        });

        return config;
    }
}

如文档中所述:

配置文件内的配置仅适用于配置文件内的地图。应用于根配置的配置适用于创建的所有映射。

并且可以创建为具有特定类型映射的类。

于 2019-11-19T13:38:39.333 回答