为什么AutoMapper int到int []数组会出现异常

nei*_*ldt 1 c# automapper

我正在使用AutoMapper对象映射器,但我得到的例外是"只有类型的顶级个人成员才支持成员的自定义配置".

基本上我有

public class Obj1 
{ 
    public int Id {get;set;} 
} 
Run Code Online (Sandbox Code Playgroud)

public class Obj2 
{ 
    public int[] Ids { get; set; } 
} 
Run Code Online (Sandbox Code Playgroud)

当我尝试创建映射时发生异常;

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

为什么会这样?我想要实现的是当对象映射时,源中的单个int Id被映射到目标int数组中的第一个元素,例如[0].完整的例外是

type ="AutoMapper.AutoMapperConfigurationException"message ="仅对类型上的顶级个人成员支持成员的自定义配置." source ="AutoMapper"
detail ="AutoMapper.AutoMapperConfigurationException:成员的自定义配置仅支持类型上的顶级单个成员.在AutoMapper.MappingExpression 2.ForMember(Expression1目标成员,Action中的AutoMapper.Impl.ReflectionHelper.FindProperty(LambdaExpression lambdaExpression).1成员选项)在......

ajg*_*ajg 6

你很接近 - 只需要制作阵列而不是设置一个单独的成员

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