Last active
September 9, 2016 02:04
-
-
Save capqueen/f9a6636247b7aed8f8ed1bc1d2bb38e1 to your computer and use it in GitHub Desktop.
AutoProfile For AutoMapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 根据IMapperTo<>接口 自动初始化AutoMapper | |
/// </summary> | |
public class AutoMapperProfile : Profile | |
{ | |
public override string ProfileName | |
{ | |
get | |
{ | |
return "AutoForIMapperTo"; | |
} | |
} | |
protected override void Configure() | |
{ | |
base.Configure(); | |
typeof(SaveBuyerDemandRequest).Assembly.GetTypes() | |
.Where(i => i.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IMapperTo<>))) | |
.ToList().ForEach(item => | |
{ | |
item.GetInterfaces() | |
.Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IMapperTo<>)) | |
.ToList() | |
.ForEach(i => { | |
var t2 = i.GetGenericArguments()[0]; | |
Mapper.CreateMap(item, t2); | |
Mapper.CreateMap(t2, item); | |
}); | |
}); | |
} | |
} | |
//Class For Example | |
public class SaveBuyerDemandRequest : IMapperTo<BuyerDemandEntity> | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment