Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extensions for automapper
public static class AutomapperExtensions
{
public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> map,
Expression<Func<TDestination, object>> selector)
{
map.ForMember(selector, options => options.Ignore());
return map;
}
public static IMappingExpression<TSource, TDestination> Define<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> map,
Expression<Func<TDestination, object>> selector1,
Expression<Func<TSource, object>> selector2)
{
return map.ForMember(selector1, options => options.MapFrom(selector2));
}
public static IMappingExpression<TSource, TDestination> Define<TSource, TDestination, TType>(
this IMappingExpression<TSource, TDestination> map,
Expression<Func<TDestination, TType?>> selector1,
Expression<Func<TSource, TType>> selector2) where TType : struct
{
var expr = selector2.Compile();
return map.ForMember(selector1, x => x.MapFrom(y => (TType?)expr(y)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.