Flattening Mapping

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Concept

By default, AutoMapper doesn’t just map properties with the same name—it also follows flattening rules:

If the source object (Person) doesn’t have a matching property on the destination (People), AutoMapper will automatically:

  1. Split nested object properties: Cn.Name → destination CnName
  2. Match parameterless methods starting with Get: GetTotal() → destination property Total

Full Code

using AutoMapper;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize AutoMapper configuration
            Mapper.Initialize(x =>
            {
                x.CreateMap();
            });

            // Source object Person
            Person p = new Person()
            {
                Age = 12,
                Cn = new Chinese() { Name = "Jack Chan" },
                Birthday = DateTime.Now,
                Sex = true,
                Salary = 5000
            };

            People peo = new People();

            // Execute mapping
            Mapper.Map(p, peo);

            Console.WriteLine(peo.CnName);  // Output: Jack Chan  [Flattening: Cn.Name → CnName]
            Console.WriteLine(peo.Total);   // Output: 100       [Flattening: GetTotal() → Total]

            Console.ReadKey();
        }
    }

    // Source class
    class Person
    {
        public int Age { get; set; }
        // Nested object
        public Chinese Cn { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }

        // Parameterless method starting with Get, automatically mapped to Total property
        public decimal GetTotal()
        {
            return 100M;
        }
    }

    public class Chinese
    {
        public string Name { get; set; }
    }

    // Destination class
    class People
    {
        public int Age { get; set; }
        // Flattened: Name property of nested object Cn, concatenated as CnName
        public string CnName { get; set; }
        public decimal Salary { get; set; }
        public bool Sex { get; set; }
        public DateTime Birthday { get; set; }
        // Flattened: matches source's GetTotal() method
        public decimal Total { get; set; }
    }
}

Summary

  1. Nested object property concatenation
    Source: Person.Cn.Name
    Destination property named CnName
    AutoMapper automatically concatenates the nested object Cn + Name and assigns it to CnName. No need to manually configure ForMember.
  2. Matching parameterless methods starting with Get
    Source method: public decimal GetTotal()
    Destination property: public decimal Total
    Strip the Get prefix, and the method’s return value is automatically assigned to Total.

Note: The method must be parameterless for this flattening rule to take effect.

Output

Jack Chan
100

Flattening Mapping

Leave a Reply

Your email address will not be published. Required fields are marked *