自动映射器 -AutoMapper.AutoMapperMappingException

Automapper -AutoMapper.AutoMapperMappingException

我正在试用 Automapper,它的映射非常简单,但它不起作用。 我正在尝试将 System.Security.Claims.Claim 类型映射到另一种类型 ClaimItem:

public class ClaimItem
{
    public string Type { get; set; }
    public string Value { get; set; }
}

但我总是得到:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: Claim -> ClaimItem System.Security.Claims.Claim -> CommonAuth.ClaimItem

Destination path: ClaimItem

Source value: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth: 05.05.2016

这是我的配置:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Claim, ClaimItem>(MemberList.Destination);
});

config.AssertConfigurationIsValid();

var cls = getClaims();
List<ClaimItem> list = new List<ClaimItem>();
cls.ForEach(cl => list.Add(Mapper.Map<ClaimItem>(cl)));

the documentation 开始,您必须从配置创建映射器。所以你应该在你的代码中有这样的

 private static Mapper _mapper;
    public static Mapper Mapper
    {
        get
        {
            if (_mapper == null)
            {
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<Claim, ClaimItem>(MemberList.Destination);
                });

                config.AssertConfigurationIsValid();
                _mapper = config.CreateMapper();
            }
            return _mapper;
        }
    }

这意味着如果你有静态 Mapper 它应该从你创建的配置中创建