@@ -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 }
0 commit comments