How to get the type of the source member when using ForAllMembers
?
E.g. when doing
public MapperConfiguration CreateMapperConfiguration<TSource, TDestination>()
{
return new MapperConfiguration(cfg =>
{
cfg.CreateMap<TSource, TDestination>().ForAllMembers(memberOptions =>
{
memberOptions.Condition((source, destination, sourceMemberValue, destinationMemberValue, resolutionContext) =>
{
if (sourceMemberValue == null)
{
// How to get the type of the source member here?
// ResolutionContext.SourceType and ResolutionContext.DestinationType don't exist.
}
else
{
var type = sourceMemberValue.GetType();
// How to find out if the type is a nullable type?
// The CLR converts int? to int when the value is boxed
// (which happens with sourceMemberValue since its
// static type is object).
}
return true;
});
});
});
}
Getting the source member type from ResolutionContext
was possible via SourceType
property in version 4 but this property doesn't exist anymore.
I checked the Upgrade guide but didn't find anything about this breaking change.
IMemberConfigurationExpression
(received as memberOptions
) has a DestinationMember
property (of type MemberInfo
) so the destination member type is available here (e.g. when casting MemberInfo
to PropertyInfo
) but there is no SourceMember
property.