Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enum to int conversion logic from VisitBinary to VisitConstant.
  • Loading branch information
ErikGjers committed Feb 16, 2023
commit 8ce9f43c026b656573d921edac125a1eeea23daf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using AutoMapper.Extensions.ExpressionMapping.Structures;
using AutoMapper.Internal;

namespace AutoMapper.Extensions.ExpressionMapping.Extensions
{
Expand Down Expand Up @@ -202,5 +203,13 @@ public static List<Type> GetUnderlyingGenericTypes(this Type type) =>
type == null || !type.GetTypeInfo().IsGenericType
? new List<Type>()
: type.GetGenericArguments().ToList();

public static bool IsEnumType(this Type type)
{
if (type.IsNullableType())
type = Nullable.GetUnderlyingType(type);

return type.IsEnum();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,6 @@ Expression DoVisitBinary(Expression newLeft, Expression newRight, Expression con
{
if (newLeft != node.Left || newRight != node.Right || conversion != node.Conversion)
{
if(node.Left.Type.IsEnum && node.Right.Type.IsEnum)
{
if (newLeft.Type.IsEnum && !newRight.Type.IsEnum)
{
newLeft = Expression.Convert(newLeft, newRight.Type);
}
else if (!newLeft.Type.IsEnum && newRight.Type.IsEnum)
{
newRight = Expression.Convert(newRight, newLeft.Type);
}
}
if (node.NodeType == ExpressionType.Coalesce && node.Conversion != null)
return Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression);
else
Expand Down Expand Up @@ -530,10 +519,16 @@ protected override Expression VisitConstant(ConstantExpression node)

if (ConfigurationProvider.CanMapConstant(node.Type, newType))
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));

else if (BothEnumOrLiteral())
return node.ConvertTypeIfNecessary(newType);
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));
}
return base.VisitConstant(node);

bool BothEnumOrLiteral()
=> (node.Type.IsLiteralType() || node.Type.IsEnumType()) && (newType.IsLiteralType() || newType.IsEnumType());
}

protected override Expression VisitMethodCall(MethodCallExpression node)
Expand Down