I have a TypeConverter
in which I'm using context.Mapper.Map()
to map two subproperties.
The properties are the same Type
and use the same (another) TypeConverter
. However in one the properties I need to pass some IMappingOperationsOptions
.
It looks like this (simplified):
public class MyTypeConverter : ITypeConverter<A, B>
{
public B Convert(A, B, ResolutionContext context)
{
var subProp1 = context.Mapper.Map<C>(B.SomeProp);
var subProp2 = context.Mapper.Map<C>(B.SomeOtherProp, ops => ops.Items["someOption"] = "someValue");
return new B
{
SubProp1 = subProp1,
SubProp2 = subProp2
};
}
}
This was working fine in AutoMapper 8.0.0
but I'm upgrading to AutoMapper 10.1.1
(last version with .NET framework support).
In this newer version of AutoMapper the overload method to pass IMappingOperationsOptions
does not exist anymore.
I could (theoretically) solve this by injecting IMapper
in the constructor of the TypeResolver
and use that instead of the ResolutionContext
's Mapper
but that doesn't feel right.
At the moment I solved the issue by temporarily updating the ResolutionContext
options, but that also doesn't really feel right.
var subProp1 = context.Mapper.Map<C>(B.SomeProp);
context.Options.Items["someOption"] = "someValue";
var subProp2 = context.Mapper.Map<C>(B.SomeOtherProp);
context.Options.Remove("someOption");
Casting ((IMapper)context.Mapper).Map()
crashes so that's not an option either. Is there a more elegant way to achieve this?