vs2013 安装 automapper

谁践踏了优雅 2022-06-10 03:24 352阅读 0赞

Center

Center 1

Install-Package AutoMapper -Version 2.1.267.0

automapper的使用

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Man man = new Man() { FirstName="liu",LastName="tian",age=10,Gender="男",weight=120,wan=true};
  6. Country country = new Country();
  7. AutoMapper.Mapper.CreateMap<Man, Person>().ConstructUsing(m=>new Person()
  8. {
  9. name =country.GetCountry(m.age)+ m.FirstName + m.LastName,
  10. xingbie=m.Gender,
  11. weight = m.weight.ToString(),
  12. gay=m.wan?"gay":"nogay"
  13. });
  14. Person p = AutoMapper.Mapper.Map<Man, Person>(man);
  15. Console.WriteLine(p.age);
  16. Console.Read();
  17. }
  18. }
  19. public class Country
  20. {
  21. public string GetCountry(int id)
  22. {
  23. return "中国";
  24. }
  25. }
  26. public class Person
  27. {
  28. public int age;
  29. public string name;
  30. public string xingbie;
  31. public string weight;
  32. public string gay;
  33. }
  34. public class Man
  35. {
  36. public string FirstName;
  37. public string LastName;
  38. public int age;
  39. public string Gender;
  40. public int weight;
  41. public bool wan; //是否是弯的
  42. }
  43. }

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读