private LambdaExpression TypeConverterMapper()
{
if(_typeMap.TypeConverterType == null)
{
return null;
}
Type type;
if(_typeMap.TypeConverterType.IsGenericTypeDefinition())
{
var genericTypeParam = _typeMap.SourceType.IsGenericType()
? _typeMap.SourceType.GetTypeInfo().GenericTypeArguments[0]
: _typeMap.DestinationTypeToUse.GetTypeInfo().GenericTypeArguments[0];
type = _typeMap.TypeConverterType.MakeGenericType(genericTypeParam);
}
else
{
type = _typeMap.TypeConverterType;
}
// (src, dest, ctxt) => ((ITypeConverter<TSource, TDest>)ctxt.Options.CreateInstance<TypeConverterType>()).ToType(src, ctxt);
var converterInterfaceType = typeof(ITypeConverter<,>).MakeGenericType(_typeMap.SourceType, _typeMap.DestinationTypeToUse);
return Lambda(
Call(
ToType(
Call(
MakeMemberAccess(_context, typeof(ResolutionContext).GetDeclaredProperty("Options")),
typeof(IMappingOperationOptions).GetDeclaredMethod("CreateInstance")
.MakeGenericMethod(type)
),
converterInterfaceType),
converterInterfaceType.GetDeclaredMethod("Convert"),
_source, _initialDestination, _context
),
_source, _initialDestination, _context);
}