Skip to content

Can’t cast a single hashtable to List of custom objects #24557

@Yaekiou

Description

@Yaekiou

Prerequisites

Steps to reproduce

Hi, A single hashtable object can be cast to System.Array of custom objects

using namespace System.Collections.Generic

class Foo {
  [int]$A
}

# OK
[Foo[]]@{ A = 1 }

# OK
[List[Foo]]@(@{ A = 2 })

# OK
[List[Foo]][Foo]@{ A = 3 }

But can’t cast to List<T>

# Error
[List[Foo]]@{ A = 4 }

Is this a bug?

Expected behavior

PS> [List[Foo]]@{ A = 4 }
A
-
4

Actual behavior

PS> [List[Foo]]@{ A = 4 }
InvalidArgument: ……

Error details

No response

Environment data

PS> $PSVersionTable
Name
Value
---
- ----
PSVersion
7.4.6
PSEdition
Core
GitCommitId
7.4.6
OS
Ubuntu 20.04.6 LTS
Platform
Unix
PSCompatibleVersions
{1.0, 2.0,
3.0, 4.0...}
PSRemotingProtocolVersion
2.3
SerializationVersion
1.1.0.1
WSManStackVersion
3.0

Visuals

IMG_0242

IMG_0241

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions