Skip to content

Commit b010cbb

Browse files
committed
use Enum.Parse<TEnum>
1 parent 4c92e3b commit b010cbb

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/AutoMapper/Mappers/EnumToEnumMapper.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ 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-
{
2217
var result = Variable(destinationType, "destinationEnumValue");
2318
var ignoreCase = True;
24-
return (Call(TryParseMethod.MakeGenericMethod(destinationType), value, ignoreCase, result), result);
19+
var tryParse = Call(TryParseMethod.MakeGenericMethod(destinationType), sourceToString, ignoreCase, result);
20+
return Block(new[] { result }, Condition(tryParse, result, Convert(sourceExpression, destinationType)));
2521
}
2622
}
2723
}

src/AutoMapper/Mappers/StringToEnumMapper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ namespace AutoMapper.Internal.Mappers
99
using static Expression;
1010
public class StringToEnumMapper : IObjectMapper
1111
{
12-
private static readonly MethodInfo EqualsMethod = typeof(StringToEnumMapper).GetMethod(nameof(StringCompareOrdinalIgnoreCase));
12+
private static readonly MethodInfo EqualsMethod = typeof(StringToEnumMapper).GetMethod("StringCompareOrdinalIgnoreCase");
13+
private static readonly MethodInfo ParseMethod = typeof(Enum).StaticGenericMethod("Parse", parametersCount: 2);
14+
private static readonly PropertyInfo Length = typeof(string).GetProperty("Length");
1315
public bool IsMatch(TypePair context) => context.SourceType == typeof(string) && context.DestinationType.IsEnum;
1416
public Expression MapExpression(IGlobalConfiguration configurationProvider, ProfileMap profileMap,
1517
MemberMap memberMap, Expression sourceExpression, Expression destExpression)
@@ -27,9 +29,10 @@ public Expression MapExpression(IGlobalConfiguration configurationProvider, Prof
2729
switchCases.Add(switchCase);
2830
}
2931
}
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);
32+
var ignoreCase = True;
33+
Expression enumParse = Call(ParseMethod.MakeGenericMethod(destinationType), sourceExpression, ignoreCase);
34+
var parse = switchCases != null ? Switch(sourceExpression, enumParse, EqualsMethod, switchCases) : enumParse;
35+
return Condition(Equal(Property(sourceExpression, Length), Zero), Default(destinationType), parse);
3336
}
3437
public static bool StringCompareOrdinalIgnoreCase(string x, string y) => StringComparer.OrdinalIgnoreCase.Equals(x, y);
3538
}

src/UnitTests/Enumerations.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
namespace AutoMapper.Tests
88
{
9+
public class InvalidStringToEnum : AutoMapperSpecBase
10+
{
11+
protected override MapperConfiguration CreateConfiguration() => new(_=> { });
12+
[Fact]
13+
public void Should_throw() => new Action(()=>Map<ConsoleColor>("d")).ShouldThrow<AutoMapperMappingException>().InnerException.Message.ShouldBe(
14+
"Requested value 'd' was not found.");
15+
}
916
public class DefaultEnumValueToString : AutoMapperSpecBase
1017
{
1118
Destination _destination;

0 commit comments

Comments
 (0)