MyTetra Share
Делитесь знаниями!
Игнорировать определенные поля при маппинге в ДТО
Время создания: 03.01.2021 21:02
Текстовые метки: Автомаппер, automapper
Раздел: Компьютер - C# - AutoMapper
Запись: Kozlov-AE/Tetra/master/base/1609696937t1tt0yzm5u/text.html на raw.githubusercontent.com

Дано:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

public class Source

    {

        public int Id { get; set; }

        public string Nickname { get; set; }

        public string Login { get; set; }

    }

 

public class Destination

    {

        public int Id { get; set; }

        public string Nickname { get; set; }

        public string Login { get; set; }

        public int Age { get; set; }

    }

Решение:



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Source source = new Source()

            {

                Id =  1,

                Login = "Login",

                Nickname =  "OldNickname"

            };

 

 

            Destination dest = new Destination()

            {

                Nickname = "NewNickName"

            };

 

            Mapper.Initialize(cfg => cfg.CreateMap<Source,Destination>()

                                       .ForMember(d => d.Nickname, (options) => options.Ignore()));

            Mapper.Map(source, dest);

 

            foreach (var property in dest.GetType().GetProperties())

            {

                Console.WriteLine($"{property.Name}:  {property.GetValue(dest)}");

            }


 
MyTetra Share v.0.59
Яндекс индекс цитирования