@@ -593,9 +593,7 @@ public static PSDataCollection<PSObject> GetPSDataCollection(object inputValue)
593
593
/// <param name="second">Object to compare first to.</param>
594
594
/// <returns>True if first is equal to the second.</returns>
595
595
public static new bool Equals ( object first , object second )
596
- {
597
- return Equals ( first , second , false , CultureInfo . InvariantCulture ) ;
598
- }
596
+ => Equals ( first , second , false , CultureInfo . InvariantCulture ) ;
599
597
600
598
/// <summary>
601
599
/// Used to compare two objects for equality converting the second to the type of the first, if required.
@@ -606,9 +604,7 @@ public static PSDataCollection<PSObject> GetPSDataCollection(object inputValue)
606
604
/// to specify the type of string comparison </param>
607
605
/// <returns>True if first is equal to the second.</returns>
608
606
public static bool Equals ( object first , object second , bool ignoreCase )
609
- {
610
- return Equals ( first , second , ignoreCase , CultureInfo . InvariantCulture ) ;
611
- }
607
+ => Equals ( first , second , ignoreCase , CultureInfo . InvariantCulture ) ;
612
608
613
609
/// <summary>
614
610
/// Used to compare two objects for equality converting the second to the type of the first, if required.
@@ -646,25 +642,28 @@ public static bool Equals(object first, object second, bool ignoreCase, IFormatP
646
642
647
643
if ( first == null )
648
644
{
649
- if ( second == null ) return true ;
650
- return false ;
645
+ return IsNullLike ( second ) ;
651
646
}
652
647
653
648
if ( second == null )
654
649
{
655
- return false ; // first is not null
650
+ return IsNullLike ( first ) ;
656
651
}
657
652
658
- string firstString = first as string ;
659
653
string secondString ;
660
- if ( firstString != null )
654
+ if ( first is string firstString )
661
655
{
662
656
secondString = second as string ?? ( string ) LanguagePrimitives . ConvertTo ( second , typeof ( string ) , culture ) ;
663
- return ( culture . CompareInfo . Compare ( firstString , secondString ,
664
- ignoreCase ? CompareOptions . IgnoreCase : CompareOptions . None ) == 0 ) ;
657
+ return culture . CompareInfo . Compare (
658
+ firstString ,
659
+ secondString ,
660
+ ignoreCase ? CompareOptions . IgnoreCase : CompareOptions . None ) == 0 ;
665
661
}
666
662
667
- if ( first . Equals ( second ) ) return true ;
663
+ if ( first . Equals ( second ) )
664
+ {
665
+ return true ;
666
+ }
668
667
669
668
Type firstType = first . GetType ( ) ;
670
669
Type secondType = second . GetType ( ) ;
@@ -708,24 +707,24 @@ public static bool Equals(object first, object second, bool ignoreCase, IFormatP
708
707
/// Helper method for [Try]Compare to determine object ordering with null.
709
708
/// </summary>
710
709
/// <param name="value">The numeric value to compare to null.</param>
711
- /// <param name="numberIsRightHandSide">True if the number to compare is on the right hand side if the comparison.</param>
710
+ /// <param name="numberIsRightHandSide">True if the number to compare is on the right hand side in the comparison.</param>
712
711
private static int CompareObjectToNull ( object value , bool numberIsRightHandSide )
713
712
{
714
713
var i = numberIsRightHandSide ? - 1 : 1 ;
715
714
716
715
// If it's a positive number, including 0, it's greater than null
717
716
// for everything else it's less than zero...
718
- switch ( value )
719
- {
720
- case Int16 i16 : return Math . Sign ( i16 ) < 0 ? - i : i ;
721
- case Int32 i32 : return Math . Sign ( i32 ) < 0 ? - i : i ;
722
- case Int64 i64 : return Math . Sign ( i64 ) < 0 ? - i : i ;
723
- case sbyte sby : return Math . Sign ( sby ) < 0 ? - i : i ;
724
- case float f : return Math . Sign ( f ) < 0 ? - i : i ;
725
- case double d : return Math . Sign ( d ) < 0 ? - i : i ;
726
- case decimal de : return Math . Sign ( de ) < 0 ? - i : i ;
727
- default : return i ;
728
- }
717
+ return value switch
718
+ {
719
+ Int16 i16 => Math . Sign ( i16 ) < 0 ? - i : i ,
720
+ Int32 i32 => Math . Sign ( i32 ) < 0 ? - i : i ,
721
+ Int64 i64 => Math . Sign ( i64 ) < 0 ? - i : i ,
722
+ sbyte s => Math . Sign ( s ) < 0 ? - i : i ,
723
+ float f => Math . Sign ( f ) < 0 ? - i : i ,
724
+ double d => Math . Sign ( d ) < 0 ? - i : i ,
725
+ decimal m => Math . Sign ( m ) < 0 ? - i : i ,
726
+ _ => IsNullLike ( value ) ? 0 : i
727
+ } ;
729
728
}
730
729
731
730
/// <summary>
@@ -741,9 +740,7 @@ private static int CompareObjectToNull(object value, bool numberIsRightHandSide)
741
740
/// to the type of <paramref name="first"/>.
742
741
/// </exception>
743
742
public static int Compare ( object first , object second )
744
- {
745
- return LanguagePrimitives . Compare ( first , second , false , CultureInfo . InvariantCulture ) ;
746
- }
743
+ => LanguagePrimitives . Compare ( first , second , false , CultureInfo . InvariantCulture ) ;
747
744
748
745
/// <summary>
749
746
/// Compare first and second, converting second to the
@@ -759,9 +756,7 @@ public static int Compare(object first, object second)
759
756
/// to the type of <paramref name="first"/>.
760
757
/// </exception>
761
758
public static int Compare ( object first , object second , bool ignoreCase )
762
- {
763
- return LanguagePrimitives . Compare ( first , second , ignoreCase , CultureInfo . InvariantCulture ) ;
764
- }
759
+ => LanguagePrimitives . Compare ( first , second , ignoreCase , CultureInfo . InvariantCulture ) ;
765
760
766
761
/// <summary>
767
762
/// Compare first and second, converting second to the
@@ -779,23 +774,20 @@ public static int Compare(object first, object second, bool ignoreCase)
779
774
/// </exception>
780
775
public static int Compare ( object first , object second , bool ignoreCase , IFormatProvider formatProvider )
781
776
{
782
- if ( formatProvider == null )
783
- {
784
- formatProvider = CultureInfo . InvariantCulture ;
785
- }
777
+ formatProvider ??= CultureInfo . InvariantCulture ;
786
778
787
779
var culture = formatProvider as CultureInfo ;
788
780
if ( culture == null )
789
781
{
790
- throw PSTraceSource . NewArgumentException ( " formatProvider" ) ;
782
+ throw PSTraceSource . NewArgumentException ( nameof ( formatProvider ) ) ;
791
783
}
792
784
793
785
first = PSObject . Base ( first ) ;
794
786
second = PSObject . Base ( second ) ;
795
787
796
788
if ( first == null )
797
789
{
798
- return second == null ? 0 : CompareObjectToNull ( second , true ) ;
790
+ return CompareObjectToNull ( second , true ) ;
799
791
}
800
792
801
793
if ( second == null )
@@ -805,7 +797,7 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
805
797
806
798
if ( first is string firstString )
807
799
{
808
- string secondString = second as string ;
800
+ var secondString = second as string ;
809
801
if ( secondString == null )
810
802
{
811
803
try
@@ -814,19 +806,26 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
814
806
}
815
807
catch ( PSInvalidCastException e )
816
808
{
817
- throw PSTraceSource . NewArgumentException ( "second" , ExtendedTypeSystem . ComparisonFailure ,
818
- first . ToString ( ) , second . ToString ( ) , e . Message ) ;
809
+ throw PSTraceSource . NewArgumentException (
810
+ nameof ( second ) ,
811
+ ExtendedTypeSystem . ComparisonFailure ,
812
+ first . ToString ( ) ,
813
+ second . ToString ( ) ,
814
+ e . Message ) ;
819
815
}
820
816
}
821
817
822
- return culture . CompareInfo . Compare ( firstString , secondString ,
823
- ignoreCase ? CompareOptions . IgnoreCase : CompareOptions . None ) ;
818
+ return culture . CompareInfo . Compare (
819
+ firstString ,
820
+ secondString ,
821
+ ignoreCase ? CompareOptions . IgnoreCase : CompareOptions . None ) ;
824
822
}
825
823
826
824
Type firstType = first . GetType ( ) ;
827
825
Type secondType = second . GetType ( ) ;
828
826
int firstIndex = LanguagePrimitives . TypeTableIndex ( firstType ) ;
829
827
int secondIndex = LanguagePrimitives . TypeTableIndex ( secondType ) ;
828
+
830
829
if ( ( firstIndex != - 1 ) && ( secondIndex != - 1 ) )
831
830
{
832
831
return LanguagePrimitives . NumericCompare ( first , second , firstIndex , secondIndex ) ;
@@ -839,8 +838,12 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
839
838
}
840
839
catch ( PSInvalidCastException e )
841
840
{
842
- throw PSTraceSource . NewArgumentException ( "second" , ExtendedTypeSystem . ComparisonFailure ,
843
- first . ToString ( ) , second . ToString ( ) , e . Message ) ;
841
+ throw PSTraceSource . NewArgumentException (
842
+ nameof ( second ) ,
843
+ ExtendedTypeSystem . ComparisonFailure ,
844
+ first . ToString ( ) ,
845
+ second . ToString ( ) ,
846
+ e . Message ) ;
844
847
}
845
848
846
849
if ( first is IComparable firstComparable )
@@ -855,7 +858,7 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
855
858
856
859
// At this point, we know that they aren't equal but we have no way of
857
860
// knowing which should compare greater than the other so we throw an exception.
858
- throw PSTraceSource . NewArgumentException ( " first" , ExtendedTypeSystem . NotIcomparable , first . ToString ( ) ) ;
861
+ throw PSTraceSource . NewArgumentException ( nameof ( first ) , ExtendedTypeSystem . NotIcomparable , first . ToString ( ) ) ;
859
862
}
860
863
861
864
/// <summary>
@@ -868,9 +871,7 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
868
871
/// zero if it is greater or zero if they are the same.</param>
869
872
/// <returns>True if the comparison was successful, false otherwise.</returns>
870
873
public static bool TryCompare ( object first , object second , out int result )
871
- {
872
- return TryCompare ( first , second , ignoreCase : false , CultureInfo . InvariantCulture , out result ) ;
873
- }
874
+ => TryCompare ( first , second , ignoreCase : false , CultureInfo . InvariantCulture , out result ) ;
874
875
875
876
/// <summary>
876
877
/// Tries to compare first and second, converting second to the type of the first, if necessary.
@@ -882,9 +883,7 @@ public static bool TryCompare(object first, object second, out int result)
882
883
/// <param name="result">Less than zero if first is smaller than second, more than zero if it is greater or zero if they are the same.</param>
883
884
/// <returns>True if the comparison was successful, false otherwise.</returns>
884
885
public static bool TryCompare ( object first , object second , bool ignoreCase , out int result )
885
- {
886
- return TryCompare ( first , second , ignoreCase , CultureInfo . InvariantCulture , out result ) ;
887
- }
886
+ => TryCompare ( first , second , ignoreCase , CultureInfo . InvariantCulture , out result ) ;
888
887
889
888
/// <summary>
890
889
/// Tries to compare first and second, converting second to the type of the first, if necessary.
@@ -900,10 +899,7 @@ public static bool TryCompare(object first, object second, bool ignoreCase, out
900
899
public static bool TryCompare ( object first , object second , bool ignoreCase , IFormatProvider formatProvider , out int result )
901
900
{
902
901
result = 0 ;
903
- if ( formatProvider == null )
904
- {
905
- formatProvider = CultureInfo . InvariantCulture ;
906
- }
902
+ formatProvider ??= CultureInfo . InvariantCulture ;
907
903
908
904
if ( ! ( formatProvider is CultureInfo culture ) )
909
905
{
@@ -988,8 +984,10 @@ public static bool TryCompare(object first, object second, bool ignoreCase, IFor
988
984
public static bool IsTrue ( object obj )
989
985
{
990
986
// null is a valid argument - it converts to false...
991
- if ( obj == null || obj == AutomationNull . Value )
987
+ if ( IsNullLike ( obj ) )
988
+ {
992
989
return false ;
990
+ }
993
991
994
992
obj = PSObject . Base ( obj ) ;
995
993
@@ -1015,8 +1013,7 @@ public static bool IsTrue(object obj)
1015
1013
if ( objType == typeof ( SwitchParameter ) )
1016
1014
return ( ( SwitchParameter ) obj ) . ToBool ( ) ;
1017
1015
1018
- IList objectArray = obj as IList ;
1019
- if ( objectArray != null )
1016
+ if ( obj is IList objectArray )
1020
1017
{
1021
1018
return IsTrue ( objectArray ) ;
1022
1019
}
@@ -1061,15 +1058,20 @@ internal static bool IsTrue(IList objectArray)
1061
1058
}
1062
1059
}
1063
1060
1061
+ /// <summary>
1062
+ /// Internal routine that determines if an object meets any of our criteria for true null.
1063
+ /// </summary>
1064
+ /// <param name="obj">The object to test.</param>
1065
+ /// <returns>True if the object is null.</returns>
1066
+ public static bool IsNull ( object obj ) => obj == null || obj == AutomationNull . Value ;
1067
+
1064
1068
/// <summary>
1065
1069
/// Internal routine that determines if an object meets any of our criteria for null.
1070
+ /// This method additionally checks for <see cref="NullString.Value"/> and <see cref="DBNull.Value"/>
1066
1071
/// </summary>
1067
1072
/// <param name="obj">The object to test.</param>
1068
1073
/// <returns>True if the object is null.</returns>
1069
- internal static bool IsNull ( object obj )
1070
- {
1071
- return ( obj == null || obj == AutomationNull . Value ) ;
1072
- }
1074
+ public static bool IsNullLike ( object obj ) => obj == DBNull . Value || obj == NullString . Value || IsNull ( obj ) ;
1073
1075
1074
1076
/// <summary>
1075
1077
/// Auxiliary for the cases where we want a new PSObject or null.
@@ -3100,15 +3102,17 @@ private static object ConvertToVoid(object valueToConvert,
3100
3102
return AutomationNull . Value ;
3101
3103
}
3102
3104
3103
- private static bool ConvertClassToBool ( object valueToConvert ,
3104
- Type resultType ,
3105
- bool recursion ,
3106
- PSObject originalValueToConvert ,
3107
- IFormatProvider formatProvider ,
3108
- TypeTable backupTable )
3105
+ private static bool ConvertClassToBool (
3106
+ object valueToConvert ,
3107
+ Type resultType ,
3108
+ bool recursion ,
3109
+ PSObject originalValueToConvert ,
3110
+ IFormatProvider formatProvider ,
3111
+ TypeTable backupTable )
3109
3112
{
3110
3113
typeConversion . WriteLine ( "Converting ref to boolean." ) ;
3111
- return valueToConvert != null ;
3114
+ // Both NullString and DBNull should be treated the same as true nulls for the purposes of this conversion.
3115
+ return ! IsNullLike ( valueToConvert ) ;
3112
3116
}
3113
3117
3114
3118
private static bool ConvertValueToBool ( object valueToConvert ,
@@ -4724,10 +4728,11 @@ internal static IConversionData FigureConversion(object valueToConvert, Type res
4724
4728
{
4725
4729
PSObject valueAsPsObj ;
4726
4730
Type originalType ;
4727
- if ( valueToConvert == null || valueToConvert == AutomationNull . Value )
4731
+
4732
+ if ( IsNull ( valueToConvert ) )
4728
4733
{
4729
- valueAsPsObj = null ;
4730
4734
originalType = typeof ( Null ) ;
4735
+ valueAsPsObj = null ;
4731
4736
}
4732
4737
else
4733
4738
{
0 commit comments