Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Common / AutoMapperConfiguration / Demos / CustomMappingDemo.cs
blob: 0ed3a96c6e031800358ee2f64cee3737e0e59a3d (plain) (blame)
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
using AutoMapper;

namespace AutoMapperConfiguration.Demos
{
    internal class DemoOrigin : IMapTo<DemoDestination>, IHaveCustomMappings
    {
        public string Origin { get; set; }

        public void CreateMappings(IMapperConfigurationExpression mapperConfig)
        {
            mapperConfig.CreateMap<DemoOrigin, DemoDestination>()
                .ForMember(destination => destination.Destination, opts => opts.MapFrom(origin => origin.Origin));
        }
    }

    internal class DemoDestination
    {
        public string Destination { get; set; }
    }

    class CustomMappingDemo
    {
        public void Run()
        {
            AutoMapperConfig.RegisterMappings(
                typeof(DemoOrigin).Assembly.GetTypes(),
                typeof(DemoDestination).Assembly.GetTypes());

            DemoOrigin origin = new DemoOrigin();
            origin.Origin = "Test";

            DemoDestination destination = origin.To<DemoDestination>();
            System.Console.WriteLine(destination.Destination); // Should be equal to "Test"
        }
    }
}