Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName - 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)
概念說明
AutoMapper 預設除了會對應同名屬性之外,還具備扁平化對應(Flattening)的規則:
當來源物件(Person)沒有與目標物件(People)名稱完全相符的屬性時,AutoMapper 會自動執行以下動作:
- 拆解來源物件中巢狀物件的屬性:
Cn.Name→ 對應到目標的CnName- 比對來源物件中以
Get開頭且無參數的方法:GetTotal()→ 對應到目標屬性Total
完整程式碼
using AutoMapper;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 初始化 AutoMapper 組態
Mapper.Initialize(x =>
{
x.CreateMap();
});
// 來源物件 Person
Person p = new Person()
{
Age = 12,
Cn = new Chinese() { Name = "Jack Chan" },
Birthday = DateTime.Now,
Sex = true,
Salary = 5000
};
People peo = new People();
// 執行對應
Mapper.Map(p, peo);
Console.WriteLine(peo.CnName); // 輸出:Jack Chan 【扁平化:Cn.Name → CnName】
Console.WriteLine(peo.Total); // 輸出:100 【扁平化:GetTotal() → Total】
Console.ReadKey();
}
}
// 來源類別
class Person
{
public int Age { get; set; }
// 巢狀物件
public Chinese Cn { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 以 Get 開頭的無參數方法,會自動對應到 Total 屬性
public decimal GetTotal()
{
return 100M;
}
}
public class Chinese
{
public string Name { get; set; }
}
// 目標類別
class People
{
public int Age { get; set; }
// 扁平化結果:將巢狀物件 Cn 的 Name 屬性拼接為 CnName
public string CnName { get; set; }
public decimal Salary { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
// 扁平化結果:對應來源的 GetTotal() 方法
public decimal Total { get; set; }
}
}
重點整理
- 巢狀物件屬性自動拼接
來源:Person.Cn.Name
目標屬性名稱:CnName
AutoMapper 會自動將巢狀物件Cn與Name拼接,並指派給CnName,不需要手動寫ForMember設定。 - 比對 Get 開頭的無參數方法
來源方法:public decimal GetTotal()
目標屬性:public decimal Total
移除Get前綴後,方法的回傳值會自動指派給Total屬性。
注意:方法必須是無參數,才會觸發這條扁平化規則。
執行結果
Jack Chan
100
扁平化對應(Flattening)