Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/System.Management.Automation/engine/LanguagePrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,7 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
}
}

string firstString = first as string;

if (firstString != null)
if (first is string firstString)
{
string secondString = second as string;
if (secondString == null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you skip this? I think we should follow one pattern or roll back the change in previous line 804.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov I think we should be careful about following one pattern too strictly. I think as string is better here because the if condition is secondString == null. Changing it to if (!(second is string secondString)) would make it less readable, which goes against our goal.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using different patterns (especially next to each other) also reduces readability - I just stumbled over it.
My suggestion was just to roll back the change at all to old pattern or use one pattern.

Expand Down Expand Up @@ -841,9 +839,7 @@ public static int Compare(object first, object second, bool ignoreCase, IFormatP
first.ToString(), second.ToString(), e.Message);
}

IComparable firstComparable = first as IComparable;

if (firstComparable != null)
if (first is IComparable firstComparable)
{
return firstComparable.CompareTo(secondConverted);
}
Expand Down Expand Up @@ -1646,9 +1642,9 @@ public static object ConvertPSObjectToType(PSObject valueToConvert, Type resultT
/// <returns></returns>
public static T ConvertTo<T>(object valueToConvert)
{
if (valueToConvert is T)
if (valueToConvert is T value)
{
return (T)valueToConvert;
return value;
}
return (T)ConvertTo(valueToConvert, typeof(T), true, CultureInfo.InvariantCulture, null);
}
Expand All @@ -1664,9 +1660,9 @@ public static T ConvertTo<T>(object valueToConvert)
/// <returns>false for conversion failure, true for success</returns>
public static bool TryConvertTo<T>(object valueToConvert, out T result)
{
if (valueToConvert is T)
if (valueToConvert is T value)
{
result = (T)valueToConvert;
result = value;
return true;
}
return TryConvertTo(valueToConvert, CultureInfo.InvariantCulture, out result);
Expand All @@ -1685,19 +1681,14 @@ public static bool TryConvertTo<T>(object valueToConvert, out T result)
public static bool TryConvertTo<T>(object valueToConvert, IFormatProvider formatProvider, out T result)
{
result = default(T);
try
{
result = (T)ConvertTo(valueToConvert, typeof(T), formatProvider);
}
catch (InvalidCastException)
{
return false;
}
catch (ArgumentException)

if (TryConvertTo(valueToConvert, typeof(T), formatProvider, out object res))
{
return false;
result = (T)res;
return true;
}
return true;

return false;
}

/// <summary>
Expand All @@ -1722,27 +1713,44 @@ public static bool TryConvertTo(object valueToConvert, Type resultType, out obje
/// <remarks>
/// This method is a variant of ConvertTo that does not throw exceptions if the conversion fails.
/// </remarks>
/// <param name="valueToConvert">value to be converted and returned</param>
/// <param name="resultType">type to convert valueToConvert</param>
/// <param name="formatProvider">governing conversion of types</param>
/// <param name="valueToConvert">value to be converted and returned.</param>
/// <param name="resultType">type to convert valueToConvert.</param>
/// <param name="formatProvider">governing conversion of types.</param>
/// <param name="result">result of the conversion. This is valid only if the return is true.</param>
/// <returns>false for conversion failure, true for success</returns>
/// <returns>false for conversion failure, true for success.</returns>
public static bool TryConvertTo(object valueToConvert, Type resultType, IFormatProvider formatProvider, out object result)
{
result = null;
try
{
result = ConvertTo(valueToConvert, resultType, formatProvider);
using (typeConversion.TraceScope("Converting \"{0}\" to \"{1}\".", valueToConvert, resultType))
{
if (resultType == null)
{
return false;
}

var conversion = FigureConversion(valueToConvert, resultType, out bool debase);
if (conversion.Rank == ConversionRank.None)
{
return false;
}

result = conversion.Invoke(
debase ? PSObject.Base(valueToConvert) : valueToConvert,
resultType,
recurse: true,
debase ? (PSObject)valueToConvert : null,
formatProvider,
backupTable: null);

return true;
}
}
catch (InvalidCastException)
{
return false;
}
catch (ArgumentException)
{
return false;
}
return true;
}

#endregion public type conversion
Expand Down