AutoMapper extension methods

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.