@@ -61,12 +61,13 @@ public object To
6161
6262 internal static bool AreValuesEqual ( object value1 , object value2 , bool convertType )
6363 {
64- if ( value1 == value2 )
64+ if ( object . Equals ( value1 , value2 ) )
6565 {
6666 return true ;
6767 }
6868
69- if ( value1 != null && value2 != null && convertType )
69+ // If they are the same type but fail with Equals check, don't bother with conversion.
70+ if ( value1 != null && value2 != null && value1 . GetType ( ) != value2 . GetType ( ) && convertType )
7071 {
7172 // Try the conversion in both ways:
7273 return ConvertTypeEquals ( value1 , value2 ) || ConvertTypeEquals ( value2 , value1 ) ;
@@ -92,19 +93,14 @@ private static bool ConvertTypeEquals(object value1, object value2)
9293
9394 private static object ConvertToEnum ( Type enumType , object value )
9495 {
95- try
96+ // value cannot be the same type of enum now
97+ return value switch
9698 {
97- if ( value is string str )
98- {
99- return Enum . Parse ( enumType , str ) ;
100- }
101-
102- return Enum . IsDefined ( enumType , value ) ? Enum . ToObject ( enumType , value ) : null ;
103- }
104- catch
105- {
106- return null ;
107- }
99+ string str when Enum . TryParse ( enumType , str , out var e ) => e , // string is most common for enum comparison
100+ _ when Type . GetTypeCode ( enumType ) == Convert . GetTypeCode ( value ) // Enum.IsDefeind only allows the same type code
101+ && Enum . IsDefined ( enumType , value ) => Enum . ToObject ( enumType , value ) ,
102+ _ => null
103+ } ;
108104 }
109105 }
110106}
0 commit comments