Skip to content

Incidentally [psobject]-wrapped reference-type instances aren't recognized as their true type (base object) by cmdlet parameters #14394

@mklement0

Description

@mklement0

Note:

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 $origHashCode

Expected 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions