DTO Framework AutoMapper, know for a long time, today, a predecessor said that it seems that the latest version can not be used, the online example is not, I downloaded the source to see a bit, pondering a bit
Wrote a simple version of the
SOURCE git address:
Https://github.com/AutoMapper/AutoMapper
1 called2DataTable Tbldatas =NewDataTable ("Datas");3TBLDATAS.COLUMNS.ADD ("ID",typeof(int));4TBLDATAS.COLUMNS.ADD ("Product",typeof(string));5TBLDATAS.COLUMNS.ADD ("Version",typeof(string));6TBLDATAS.COLUMNS.ADD ("Description",typeof(string));7TBLDATAS.ROWS.ADD (New Object[] {1,"a","b","C" });8TBLDATAS.ROWS.ADD (New Object[] {2,"a","b","C" });9TBLDATAS.ROWS.ADD (New Object[] {3,"a","b","C" });TenTBLDATAS.ROWS.ADD (New Object[] {4,"a","b","C" }); OneTBLDATAS.ROWS.ADD (New Object[] {5,"a","b","C" }); A -Mapper.initialize (config = - { theConfig. Createmapinformat<datarow, source>(); -Config. Createmapinformat<source, target>(); - }); - varSource = Tbldatas.rows.oftype<datarow> (). Select (item = Mapper.map<datarow, source>(item)). ToList (); + varlist = Mapper.map<list<source>, list<target>> (Source);
1 the corresponding entity class code2 Public classTarget3 {4 Public intID {Get;Set; }5 Public stringProduct {Get;Set; }6 Public stringVersion {Get;Set; }7 Public stringDescription {Get;Set; }8 }9 Ten Public classSource One { A Public intID {Get;Set; } - Public stringProduct {Get;Set; } - Public stringVersion {Get;Set; } the Public stringDescription {Get;Set; } -}
Extension methods
1 Public Static classMapperextens2 {3 Public Static voidCreatemapinformat<tsource, Tdestination> ( Thisimapperconfigurationexpression config)4 {5 varSourceType =typeof(TSource);6 vardestinationtype =typeof(tdestination);7 8Config. CreateProfile (Sourcetype.name, (profile) =9 {Ten if(Sourcetype.isgenerictype) One { ASourceType =sourcetype.getgenerictypedefinition (); - } - the if(Destinationtype.isgenerictype) - { -destinationtype =destinationtype.getgenerictypedefinition (); - } + - varMap = profile. Createmap<tsource, tdestination>(); + foreach(varPropertyinchdestinationtype.getproperties ()) A { atMap. Formember (property. Name, opt + = opt. Mapfrom (s = =Reflex (S, property))); - } - - }); - } - in Private Static ObjectReflex<tsource>(TSource source, PropertyInfo property) - { to returnDefaultfortype (source, property); + } - the Private Static ObjectDefaultfortype<tsource>(TSource source, PropertyInfo property) * { $ if(source. GetType () = =typeof(DataRow))Panax Notoginseng { - return(Source asDataRow)?[Property. Name]; the } + A returnFindpropertyinfo (source, property); the } + - Private Static ObjectFindpropertyinfo<tsource>(TSource source, PropertyInfo property) $ { $ if(typeof(TSource). Isvaluetype) - { - returnActivator.CreateInstance (property. PropertyType); the } - ElseWuyi { the return typeof(TSource). GetProperty (property. Name)?. GetValue (source); - } Wu } -}
AutoMapper extension methods