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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
Note: The method must be parameterless for this flattening rule to take effect.
Output
Jack Chan
100
Flattening Mapping
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
Note: The method must be parameterless for this flattening rule to take effect.
Output
Jack Chan
100
Flattening Mapping
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
Note: The method must be parameterless for this flattening rule to take effect.
Output
Jack Chan
100
Flattening Mapping
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
Note: The method must be parameterless for this flattening rule to take effect.
Output
Jack Chan
100
Flattening Mapping
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
Note: The method must be parameterless for this flattening rule to take effect.
Output
Jack Chan
100
Flattening Mapping
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName - Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
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:
- Split nested object properties:
Cn.Name→ destinationCnName- Match parameterless methods starting with
Get:GetTotal()→ destination propertyTotal
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
- Nested object property concatenation
Source:Person.Cn.Name
Destination property namedCnName
AutoMapper automatically concatenates the nested objectCn+Nameand assigns it toCnName. No need to manually configureForMember. - Matching parameterless methods starting with Get
Source method:public decimal GetTotal()
Destination property:public decimal Total
Strip theGetprefix, and the method’s return value is automatically assigned toTotal.
Note: The method must be parameterless for this flattening rule to take effect.
Output
Jack Chan
100
Flattening Mapping