Пример #1
0
    public void Test_EmitMapper_Map_ListObject(List <FromClass> listFrom)
    {
        _testOutputHelper.WriteLine(listFrom.Count.ToString());

        var rw1 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                typeof(FromClass).GetMember(nameof(FromClass.Inner))[0].AsEnumerable(
                    typeof(FromClass.InnerClass).GetMember(nameof(FromClass.Inner.Message))[0])),
            Destination = new MemberDescriptor(
                typeof(ToClass).GetMember(nameof(ToClass.Message))[0].AsEnumerable())
        };

        var rw2 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                typeof(FromClass).GetMember(nameof(FromClass.Inner))[0].AsEnumerable(
                    typeof(FromClass.InnerClass).GetMember(nameof(FromClass.InnerClass.GetMessage2))[0])),
            Destination = new MemberDescriptor(
                typeof(ToClass).GetMember(nameof(ToClass.Message2))[0].AsEnumerable())
        };

        var mapper = Mapper.Default.GetMapper <List <FromClass>, List <ToClass> >(
            new CustomMapConfig {
            GetMappingOperationFunc = (from, to) => rw1.AsEnumerable(rw2)
        });

        var tolist = mapper.Map(listFrom);

        using var f = listFrom.GetEnumerator();
        using var t = tolist.GetEnumerator();

        while (f.MoveNext() && t.MoveNext())
        {
            _testOutputHelper.WriteLine(t.Current.Message);
            f.Current.Inner.Message.ShouldBe(t.Current.Message);
            f.Current.Inner.GetMessage2().ShouldBe(t.Current.Message2);
        }
    }
Пример #2
0
    public void TestFlattering1()
    {
        var rw1 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                new[]
            {
                typeof(Source).GetMember(nameof(Source.InnerSource))[0],
                typeof(Source.InnerSourceClass).GetMember(nameof(Source.InnerSource.Message))[0]
            }),
            Destination = new MemberDescriptor(
                new[] { typeof(Destination).GetMember(nameof(Destination.Message))[0] })
        };

        var rw2 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                new[]
            {
                typeof(Source).GetMember(nameof(Source.InnerSource))[0],
                typeof(Source.InnerSourceClass).GetMember(nameof(Source.InnerSourceClass.GetMessage2))[0]
            }),
            Destination = new MemberDescriptor(
                new[] { typeof(Destination).GetMember(nameof(Destination.Message2))[0] })
        };

        var mapper = Mapper.Default.GetMapper <Source, Destination>(
            new CustomMapConfig {
            GetMappingOperationFunc = (from, to) => rw1.AsEnumerable(rw2)
        });

        var b      = new Source();
        var result = mapper.Map(b);

        b.InnerSource.Message.ShouldBe(result.Message);
        b.InnerSource.GetMessage2().ShouldBe(result.Message2);
    }