@@ -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