I have some Automapper profiles, and I create two different mapper instances at runtime according to the situations. I need to ignore some members mapped inside these profiles at runtime for one of the mapper instances. Considering the example below how can I achieve this?
public class Foo
{
public object SomeField { get; set; }
}
public class FooDto
{
public object SomeField { get; set; }
}
public class FooProfile : Profile
{
public FooProfile()
{
CreateMap<Foo, FooDto>();
}
}
Create multiple mapper instances using the profile, and with different mapping expressions, at runtime:
public class MapperFactory
{
public IMapper Create(bool isSomeFieldIgnored)
{
MapperConfiguration configuration;
if (isSomeFieldIgnored)
{
configuration = new MapperConfiguration(expression =>
{
expression.AddProfile<FooProfile>();
// ignore the SomeField here!
});
}
else
{
configuration = new MapperConfiguration(expression =>
{
expression.AddProfile<FooProfile>();
});
}
return configuration.CreateMapper();
}
}