Skip to content

Instantly share code, notes, and snippets.

@capqueen
Last active September 9, 2016 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capqueen/f9a6636247b7aed8f8ed1bc1d2bb38e1 to your computer and use it in GitHub Desktop.
Save capqueen/f9a6636247b7aed8f8ed1bc1d2bb38e1 to your computer and use it in GitHub Desktop.
AutoProfile For AutoMapper
/// <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