Automapper - concrete object to array

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 15:23:03

I found a good solution. Using the ConstructUsing feature is the way to go.

    [Test]
    public void CanConvertEmployeeToArray()
    {

        var employee = new Employee()
        {
            name = "test",
            age = 20,
            cars = 1
        };

        Mapper.CreateMap<Employee, int[]>().ConstructUsing(
                x => new int[] { x.age, x.cars }
            );

        var array = Mapper.Map<Employee, int[]>(employee);

        Assert.That(employee.age, Is.EqualTo(array[0]));
        Assert.That(employee.cars, Is.EqualTo(array[1]));

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