diff --git a/src/PowerShellEditorServices/Debugging/VariableDetails.cs b/src/PowerShellEditorServices/Debugging/VariableDetails.cs index 46c3c32b1..720c445c5 100644 --- a/src/PowerShellEditorServices/Debugging/VariableDetails.cs +++ b/src/PowerShellEditorServices/Debugging/VariableDetails.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Management.Automation; using System.Reflection; +using Microsoft.PowerShell.EditorServices.Utility; namespace Microsoft.PowerShell.EditorServices { @@ -159,44 +160,49 @@ private static string GetValueString(object value, bool isExpandable) entry.Key, GetValueString(entry.Value, GetIsExpandable(entry.Value))); } - else if (value.ToString().Equals(objType.ToString())) + else { - // If the ToString() matches the type name, then display the type - // name in PowerShell format. - string shortTypeName = objType.Name; + string valueToString = value.SafeToString(); - // For arrays and ICollection, display the number of contained items. - if (value is Array) + if (valueToString.Equals(objType.ToString())) { - var arr = value as Array; - if (arr.Rank == 1) + // If the ToString() matches the type name, then display the type + // name in PowerShell format. + string shortTypeName = objType.Name; + + // For arrays and ICollection, display the number of contained items. + if (value is Array) { - shortTypeName = InsertDimensionSize(shortTypeName, arr.Length); + var arr = value as Array; + if (arr.Rank == 1) + { + shortTypeName = InsertDimensionSize(shortTypeName, arr.Length); + } } + else if (value is ICollection) + { + var collection = (ICollection)value; + shortTypeName = InsertDimensionSize(shortTypeName, collection.Count); + } + + valueString = "[" + shortTypeName + "]"; } - else if (value is ICollection) + else { - var collection = (ICollection)value; - shortTypeName = InsertDimensionSize(shortTypeName, collection.Count); + valueString = valueToString; } - - valueString = "[" + shortTypeName + "]"; - } - else - { - valueString = value.ToString(); } } else { - // ToString() output is not the typename, so display that as this object's value + // Value is a scalar (not expandable). If it's a string, display it directly otherwise use SafeToString() if (value is string) { valueString = "\"" + value + "\""; } else { - valueString = value.ToString(); + valueString = value.SafeToString(); } } diff --git a/src/PowerShellEditorServices/PowerShellEditorServices.csproj b/src/PowerShellEditorServices/PowerShellEditorServices.csproj index ef6e2f1fa..684ad5050 100644 --- a/src/PowerShellEditorServices/PowerShellEditorServices.csproj +++ b/src/PowerShellEditorServices/PowerShellEditorServices.csproj @@ -120,6 +120,7 @@ + diff --git a/src/PowerShellEditorServices/Utility/Extensions.cs b/src/PowerShellEditorServices/Utility/Extensions.cs new file mode 100644 index 000000000..432b0c830 --- /dev/null +++ b/src/PowerShellEditorServices/Utility/Extensions.cs @@ -0,0 +1,34 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; + +namespace Microsoft.PowerShell.EditorServices.Utility +{ + internal static class ObjectExtensions + { + /// + /// Extension to evaluate an object's ToString() method in an exception safe way. This will + /// extension method will not throw. + /// + /// The object on which to call ToString() + /// The ToString() return value or a suitable error message is that throws. + public static string SafeToString(this object obj) + { + string str; + + try + { + str = obj.ToString(); + } + catch (Exception ex) + { + str = $""; + } + + return str; + } + } +}