vs2013 安裝 automapper


Install-Package AutoMapper -Version 2.1.267.0


automapper的使用

class Program
    {
        static void Main(string[] args)
        {
            Man man = new Man() { FirstName="liu",LastName="tian",age=10,Gender="男",weight=120,wan=true};
            Country country = new Country();
            AutoMapper.Mapper.CreateMap<Man, Person>().ConstructUsing(m=>new Person() 
            {
                name =country.GetCountry(m.age)+ m.FirstName + m.LastName,
                xingbie=m.Gender,
                weight = m.weight.ToString(),
                gay=m.wan?"gay":"nogay"
            });
            Person p = AutoMapper.Mapper.Map<Man, Person>(man);
            Console.WriteLine(p.age);
            Console.Read();
        }
      }

    public class Country
    {
        public string GetCountry(int id)
        {
            return "中國";
        }
    }
    public class Person
    {
        public int age;
        public string name;
        public string xingbie;
        public string weight;
        public string gay;
    }

    public class Man
    {
        public string FirstName;
        public string LastName;
        public int age;
        public string Gender;
        public int weight;
        public bool wan;  //是否是彎的
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章