Skip to content

v5 Regression: Casting object[] to more narrow generic collection fails with a single element, but not with two #2208

Closed

Description

@oising

Steps to reproduce

> [Collections.Generic.List[int]]@(1)

or

> [Management.Automation.LanguagePrimitives]::ConvertTo(@(1), [Collections.Generic.List[int]])

Note that casting @(1,2) will succeed. Only the case @(1) fails. This is a regression because it functions correctly in v4.

Expected behavior

Casts to an instance of List<int> containing a single element: 1

Actual behavior

Exception calling "ConvertTo" with "2" argument(s): "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32"."

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.0.10586.494
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.494
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. added
    Issue-Bug
    WG-Engine
    on Sep 8, 2016
  2. vors commented on Sep 8, 2016

    @vors
    Collaborator

    Confirmed. Thank you, Oisin Grehan (@oising) !

  3. oising commented on Sep 8, 2016

    @oising
    ContributorAuthor

    Workaround

    Given that [System.Collections.Generic.List[int]]1 works (remove the array wrapper), it can be coerced to work generally by wrapping the target in a subexpression:

    > $a = @(1)
    > [System.Collections.Generic.List[int]]$($a)
    1
    > $a = @(1,2,3)
    > [System.Collections.Generic.List[int]]$($a)
    1
    2
    3
  4. added this to the milestone on Nov 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugWG-Engine

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions