Why do I get exception for AutoMapper int to int[] array

一个人想着一个人 提交于 2019-12-23 03:26:33

问题


I'm using the AutoMapper object mapper, but am getting the exception "Custom configuration for members is only supported for top-level individual members on a type."

Basically I have

public class Obj1 
{ 
    public int Id {get;set;} 
} 

and

public class Obj2 
{ 
    public int[] Ids { get; set; } 
} 

Th exception occurs when I try to create the mapping like;

Mapper
    .CreateMap<Obj1, Obj2>()
    .ForMember(d => d.Ids[0], o => o.MapFrom(s => s.Id)
);

Why is this happening ? What I'm wanting to achieve is when the objects are mapping that the single int Id in the source is mapped to the first element in the destination int array e.g [0]. The complete exception is

type="AutoMapper.AutoMapperConfigurationException" message="Custom configuration for members is only supported for top-level individual members on a type." source="AutoMapper"
detail="AutoMapper.AutoMapperConfigurationException: Custom configuration for members is only supported for top-level individual members on a type. at AutoMapper.Impl.ReflectionHelper.FindProperty(LambdaExpression lambdaExpression) at AutoMapper.MappingExpression2.ForMember(Expression1 destinationMember, Action`1 memberOptions) at ...


回答1:


You are close - just need to make the array instead of setting a individual member

Mapper.CreateMap<Obj1, Obj2>().ForMember(d => d.Ids, o => o.MapFrom(s => new[]{s.Id}));


来源:https://stackoverflow.com/questions/23088796/why-do-i-get-exception-for-automapper-int-to-int-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!