-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Lists.cs
99 lines (85 loc) · 2.23 KB
/
Lists.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections.Generic;
using AutoMapper;
using Should;
using NUnit.Framework;
using System.Linq;
namespace AutoMapperSamples.Mappers
{
namespace Lists
{
[TestFixture]
public class SimpleExample
{
public class Source
{
public int Value { get; set; }
}
public class Destination
{
public int Value { get; set; }
}
[Test]
public void Example()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Source, Destination>();
});
var sources = new[]
{
new Source {Value = 5},
new Source {Value = 6},
new Source {Value = 7}
};
IEnumerable<Destination> ienumerableDest = Mapper.Map<Source[], IEnumerable<Destination>>(sources);
ICollection<Destination> icollectionDest = Mapper.Map<Source[], ICollection<Destination>>(sources);
IList<Destination> ilistDest = Mapper.Map<Source[], IList<Destination>>(sources);
List<Destination> listDest = Mapper.Map<Source[], List<Destination>>(sources);
ienumerableDest.Count().ShouldEqual(3);
icollectionDest.Count().ShouldEqual(3);
ilistDest.Count().ShouldEqual(3);
listDest.Count().ShouldEqual(3);
}
}
[TestFixture]
public class PolymorphicExample
{
public class ParentSource
{
public int Value1 { get; set; }
}
public class ChildSource : ParentSource
{
public int Value2 { get; set; }
}
public class ParentDestination
{
public int Value1 { get; set; }
}
public class ChildDestination : ParentDestination
{
public int Value2 { get; set; }
}
[Test]
public void Example()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ParentSource, ParentDestination>()
.Include<ChildSource, ChildDestination>();
cfg.CreateMap<ChildSource, ChildDestination>();
});
var sources = new[]
{
new ParentSource(),
new ChildSource(),
new ParentSource()
};
var destinations = Mapper.Map<ParentSource[], ParentDestination[]>(sources);
destinations[0].ShouldBeType<ParentDestination>();
destinations[1].ShouldBeType<ChildDestination>();
destinations[2].ShouldBeType<ParentDestination>();
}
}
}
}