自动映射器反向映射不会自动映射接口



我创建了一个简单的测试用例来重现我的问题。给定以下两个接口

    public interface IOne
    {
        int Id { get; }
        string PropOne { get; }
        string PropTwo { get; }
        string PropThree { get; }
    }
    public interface ITwo
    {
        int Id { get; }
        string PropOne { get; }
        string PropTwo { get; }
        string PropThree { get; }
    }

以及下面的映射代码和测试

        Mapper.CreateMap<IOne, ITwo>()
              .ReverseMap()
            ;
        Mapper.AssertConfigurationIsValid();

我得到这个例外。

AutoMapper.AutoMapperConfigurationException: 
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
===============================================================================================================================================================================================================
ITwo -> IOne (Source member list)
Vertafore.Services.Producer.DomainModels.ApplicationService.Test.Map.DomainAutoMapperProfileTest+ITwo ->     Vertafore.Services.Producer.DomainModels.ApplicationService.Test.Map.DomainAutoMapperProfileTest+IOne (Source member list)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Id
PropOne
PropTwo
PropThree

我错过了一些明显的东西吗?据我了解,只要您的属性名称相同,CreateMap 就会自动为您的属性创建映射。

从接口映射到接口时,自动映射不起作用吗?

AutoMapper 需要 setter 来执行映射。将这些添加到接口有助于:

public interface IOne
{
    int Id { get; set; }
    string PropOne { get; set; }
    string PropTwo { get; set; }
    string PropThree { get; set; }
}
public interface ITwo
{
    int Id { get; set; }
    string PropOne { get; set; }
    string PropTwo { get; set; }
    string PropThree { get; set; }
}

我希望这有所帮助。

相关内容

  • 没有找到相关文章

最新更新