This example takes the example of ASP. WebForm:
Structure:
Main code: Automapperconfig class
public class Automapperconfig
{
public static void Registerautomapper ()
{
AutoMapper.Configuration.Configure ();
}
}
Configuration class:
public static void Configure ()
{
Mapper.initialize (cfg =
{
var typetoaddprofile = assembly.getexecutingassembly (). GetTypes ();
foreach (var item in Typetoaddprofile)
{
if (item. BaseType! = NULL && item. Basetype.name = = "Profile" &&!item. BaseType.Name.Equals ("Configuration"))
{
Object o = Activator.CreateInstance (item);
Cfg. Addprofile (o as Profile);
}
}
});
Mapper.assertconfigurationisvalid ();
}
Profiles under Testprofiles class:
public class Testprofiles:profile
{
protected override void Configure ()
{
Createmap<testmodel, dto_testmodel> ();
//. Formember (a = a.address, B = b.mapfrom (c = c.name));
}
}
In Golbal:
protected void Application_Start (object sender, EventArgs e)
{
AutoMapperWeb.AutoMapperConfig.RegisterAutoMapper ();
}
protected void Page_Load (object sender, EventArgs e)
{
var T1 = new List<testmodel> ()
{
New Testmodel () {id=1,name= "Zhang San", age=25,sex= "male", address= "Changsha Yuhua in Hunan Province"},
New Testmodel () {id=1,name= "John Doe", age=25,sex= "male", address= "Changsha Yuhua Future City", Fang "qq=", 9375671 "[Email email=] Phone= "15725032"}
};
var list = Mapper.map<list<testmodel>, list<dto_testmodel>> (T1);
String str = string. Format ("<div>foreach (var item in list)
{
str + = string. Format ("Name:{0},<br/>", item.) Name);
str + = string. Format ("Home Address:{0},<br/>", item. Address);
str + = string. Format ("Qq:{0},<br/>", item. QQ);
str + = string. Format ("Mailbox:{0}}
str + = string. Format ("</div>");
Response.Write (str);
Response.End ();
}
AutoMapper First Use Experience