-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Issue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime
Description
Note:
-
The problem was first discovered by @AlexKichkailo in Pass parameter by reference to a cmdlet written in C# #14384
-
It looks like another manifestation of Objects are situationally invisibly [psobject]-wrapped, sometimes causing unexpected behavior. #5579. That is, the usually invisible
[psobject]wrapper that cmdlets wrap around their output objects causes cmdlet parameters not to recognize such wrapped instances as their underlying type (base object).
Note that parameters declared in PowerShell code (scripts, functions) are not affected.
Steps to reproduce
# Create a cmdlet that has a [System.Collections.ArrayList] parameter.
Add-Type @'
using System;
using System.Collections;
using System.Management.Automation;
[Cmdlet(VerbsCommon.Get, "HashCode")]
public class AddCollectionItemCmdlet : Cmdlet
{
[Parameter]
public ArrayList Collection { get; set; }
protected override void BeginProcessing()
{
WriteObject(Collection.GetHashCode());
}
}
'@ -PassThru | % Assembly | Import-Module
# Use New-Object to create an array list.
# Note: Unlike when you use the static ::new() method, you get an instance
# that is *wrapped in [psobject]*.
$al = New-Object System.Collections.ArrayList
$origHashCode = $al.GetHashCode()
# OK: Bypassing the [psobject] wrapper.
Get-HashCode -Collection $al.psobject.BaseObject | Should -Be $origHashCode
# !! BROKEN: Seemingly the array list is *cloned* if the input object
# !! is [psobject]-wrapped.
Get-HashCode -Collection $al | Should -Be $origHashCodeExpected behavior
Both tests should pass.
Actual behavior
The second test fails, because the [psobject]-wrapped input [System.Collections.ArrayList] instance was seemingly cloned behind the scenes, so the hash codes differ.
Environment data
PowerShell Core 7.2.0-preview.1
AlexKichkailo and iRon7
Metadata
Metadata
Assignees
Labels
Issue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime