8000 Handle `PSObject` argument specially in method invocation logging (#1… · PowerShell/PowerShell@3fbd90a · GitHub
[go: up one dir, main page]

< 8000 a href="#start-of-content" data-skip-target-assigned="false" class="px-2 py-4 color-bg-accent-emphasis color-fg-on-emphasis show-on-focus js-skip-to-content">Skip to content

Commit 3fbd90a

Browse files
authored
Handle PSObject argument specially in method invocation logging (#18060)
1 parent eb92c50 commit 3fbd90a

File tree

1 file changed

+34
-1
lines changed
  • src/System.Management.Automation/engine/runtime/Operations

1 file changed

+34
-1
lines changed

src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Management.Automation.Internal.Host;
1414
using System.Management.Automation.Language;
1515
using System.Management.Automation.Runspaces;
16+
using System.Numerics;
1617
using System.Reflection;
1718
using System.Runtime.CompilerServices;
1819
using System.Text.RegularExpressions;
@@ -3545,6 +3546,38 @@ internal static class MemberInvocationLoggingOps
35453546
}
35463547
);
35473548

3549+
private static string ArgumentToString(object arg)
3550+
{
3551+
object baseObj = PSObject.Base(arg);
3552+
if (baseObj is null)
3553+
{
3554+
// The argument is null or AutomationNull.Value.
3555+
return "null";
3556+
}
3557+
3558+
// The comparisons below are ordered by the likelihood of arguments being of those types.
3559+
if (baseObj is string str)
3560+
{
3561+
return str;
3562+
}
3563+
3564+
// Special case some types to call 'ToString' on the object. For the rest, we return its
3565+
// full type name to avoid calling a potentially expensive 'ToString' implementation.
3566+
Type baseType = baseObj.GetType();
3567+
if (baseType.IsEnum || baseType.IsPrimitive
3568+
|| baseType == typeof(Guid)
3569+
|| baseType == typeof(Uri)
3570+
|| baseType == typeof(Version)
3571+
|| baseType == typeof(SemanticVersion)
3572+
|| baseType == typeof(BigInteger)
3573+
|| baseType == typeof(decimal))
3574+
{
3575+
return baseObj.ToString();
3576+
}
3577+
3578+
return baseType.FullName;
3579+
}
3580+
35483581
internal static void LogMemberInvocation(string targetName, string name, object[] args)
35493582
{
35503583
try
@@ -3554,7 +3587,7 @@ internal static void LogMemberInvocation(string targetName, string name, object[
35543587

35553588
for (int i = 0; i < args.Length; i++)
35563589
{
3557-
string value = args[i] is null ? "null" : args[i].ToString();
3590+
string value = ArgumentToString(args[i]);
35583591

35593592
if (i > 0)
35603593
{

0 commit comments

Comments
 (0)
0