Skip to content

Commit 4c92e3b

Browse files
committed
use Enum.TryParse<TEnum>
1 parent b8f8546 commit 4c92e3b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/AutoMapper/Mappers/EnumToEnumMapper.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ public Expression MapExpression(IGlobalConfiguration configurationProvider, Prof
1414
{
1515
var destinationType = destExpression.Type;
1616
var sourceToString = Call(sourceExpression, ObjectToString);
17+
var (tryParse, result) = TryParse(destinationType, sourceToString);
18+
return Block(new[] { result }, Condition(tryParse, result, Convert(sourceExpression, destinationType)));
19+
}
20+
internal static (MethodCallExpression TryParse, ParameterExpression Result) TryParse(Type destinationType, Expression value)
21+
{
1722
var result = Variable(destinationType, "destinationEnumValue");
1823
var ignoreCase = True;
19-
var tryParse = Call(TryParseMethod.MakeGenericMethod(destinationType), sourceToString, ignoreCase, result);
20-
return Block(new[] { result }, Condition(tryParse, result, Convert(sourceExpression, destinationType)));
24+
return (Call(TryParseMethod.MakeGenericMethod(destinationType), value, ignoreCase, result), result);
2125
}
2226
}
2327
}

src/AutoMapper/Mappers/StringToEnumMapper.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ namespace AutoMapper.Internal.Mappers
99
using static Expression;
1010
public class StringToEnumMapper : IObjectMapper
1111
{
12-
private static readonly MethodInfo EqualsMethod = typeof(StringToEnumMapper).GetMethod("StringCompareOrdinalIgnoreCase");
13-
private static readonly MethodInfo ParseMethod = typeof(Enum).GetMethod("Parse", new[] { typeof(Type), typeof(string), typeof(bool) });
14-
private static readonly MethodInfo IsNullOrEmptyMethod = typeof(string).GetMethod("IsNullOrEmpty");
12+
private static readonly MethodInfo EqualsMethod = typeof(StringToEnumMapper).GetMethod(nameof(StringCompareOrdinalIgnoreCase));
1513
public bool IsMatch(TypePair context) => context.SourceType == typeof(string) && context.DestinationType.IsEnum;
1614
public Expression MapExpression(IGlobalConfiguration configurationProvider, ProfileMap profileMap,
1715
MemberMap memberMap, Expression sourceExpression, Expression destExpression)
@@ -29,9 +27,9 @@ public Expression MapExpression(IGlobalConfiguration configurationProvider, Prof
2927
switchCases.Add(switchCase);
3028
}
3129
}
32-
var enumParse = ToType(Call(ParseMethod, Constant(destinationType), sourceExpression, True), destinationType);
33-
var parse = switchCases != null ? Switch(sourceExpression, enumParse, EqualsMethod, switchCases) : enumParse;
34-
return Condition(Call(IsNullOrEmptyMethod, sourceExpression), Default(destinationType), parse);
30+
var (tryParse, result) = EnumToEnumMapper.TryParse(destinationType, sourceExpression);
31+
var returnValue = switchCases != null ? Switch(sourceExpression, result, EqualsMethod, switchCases) : (Expression)result;
32+
return Block(new[] { result }, tryParse, returnValue);
3533
}
3634
public static bool StringCompareOrdinalIgnoreCase(string x, string y) => StringComparer.OrdinalIgnoreCase.Equals(x, y);
3735
}

0 commit comments

Comments
 (0)