AutoMapper擴充方法

來源:互聯網
上載者:User

標籤:conf   code   row   init   簡易   col   lex   reac   set   

DTO架構AutoMapper,知道很久了,今天有個前輩說好像最新版本不能用了,網上樣本不行了,自己下載源碼看了一下,琢磨了一下

寫了一個簡易版的

源git地址:

https://github.com/AutoMapper/AutoMapper

 

 1 調用 2             DataTable tblDatas = new DataTable("Datas"); 3             tblDatas.Columns.Add("ID", typeof(int)); 4             tblDatas.Columns.Add("Product", typeof(string)); 5             tblDatas.Columns.Add("Version", typeof(string)); 6             tblDatas.Columns.Add("Description", typeof(string)); 7             tblDatas.Rows.Add(new object[] { 1, "a", "b", "c" }); 8             tblDatas.Rows.Add(new object[] { 2, "a", "b", "c" }); 9             tblDatas.Rows.Add(new object[] { 3, "a", "b", "c" });10             tblDatas.Rows.Add(new object[] { 4, "a", "b", "c" });11             tblDatas.Rows.Add(new object[] { 5, "a", "b", "c" });12 13             Mapper.Initialize(config =>14             {15                 config.CreateMapInformat<DataRow, Source>();16                 config.CreateMapInformat<Source, Target>();17             });18             var source = tblDatas.Rows.OfType<DataRow>().Select(item => Mapper.Map<DataRow, Source>(item)).ToList();19             var list = Mapper.Map<List<Source>, List<Target>>(source);
 1 對應的實體類代碼 2     public class Target 3     { 4         public int ID { get; set; } 5         public string Product { get; set; } 6         public string Version { get; set; } 7         public string Description { get; set; } 8     } 9 10     public class Source11     {12         public int ID { get; set; }13         public string Product { get; set; }14         public string Version { get; set; }15         public string Description { get; set; }16     }

 

擴充方法

 1     public static class MapperExtens 2     { 3         public static void CreateMapInformat<TSource, TDestination>( this IMapperConfigurationExpression config) 4         { 5             var sourceType = typeof(TSource); 6             var destinationType = typeof(TDestination); 7  8             config.CreateProfile(sourceType.Name, (profile) => 9             {10                 if (sourceType.IsGenericType)11                 {12                     sourceType = sourceType.GetGenericTypeDefinition();13                 }14 15                 if (destinationType.IsGenericType)16                 {17                     destinationType = destinationType.GetGenericTypeDefinition();18                 }19 20                 var map = profile.CreateMap<TSource, TDestination>();21                 foreach (var property in destinationType.GetProperties())22                 {23                     map.ForMember(property.Name, opt => opt.MapFrom(s => Reflex(s, property)));24                 }25 26             });27         }28 29         private static object Reflex<TSource>(TSource source, PropertyInfo property)30         {31             return DefaultForType(source, property);32         }33 34         private static object DefaultForType<TSource>(TSource source, PropertyInfo property)35         {36             if (source.GetType() == typeof(DataRow))37             {38                 return (source as DataRow)?[property.Name];39             }40 41             return FindPropertyInfo(source, property);  42         }43 44         private static object FindPropertyInfo<TSource>(TSource source, PropertyInfo property)45         {46             if (typeof(TSource).IsValueType)47             {48                 return Activator.CreateInstance(property.PropertyType);49             }50             else51             {52                 return typeof(TSource).GetProperty(property.Name)?.GetValue(source);53             }54         }55     }

 

AutoMapper擴充方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.