使用 OutVariable 创建 ArrayList [英] Using OutVariable Creates ArrayList

查看:43
本文介绍了使用 OutVariable 创建 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定发生这种情况是有正当理由的,但我不知道是什么原因.我有以下代码

I'm sure there is a valid reason why this happens but I don't know what it is. I have the following code

$Deleted = $Items[0].ParentNode.RemoveChild($Items[0])
Write-Output $Deleted

如果我使用

Do-Something -OutVariable Test

我按预期在屏幕和变量中获得输出,但变量是一个 ArraryList,其中包含一个 XmlElement

I get the output on the screen and in the variable as expected, but the variable is an ArraryList with a single element that is of type XmlElement

如果我跑了

$Test = Do-Something

我没有按预期在屏幕上得到输出,返回的输出是 XmlElement

I don't get the output on the screen, as expected, and the returned output is a single object of type XmlElement

为什么 OutVariable 返回一个 ArrayList 而不是返回函数返回的单个项目?

Why does OutVariable return an ArrayList rather than return the single item the function returns?

谢谢

推荐答案

从 PowerShell [Core] 7.0 开始:

As of PowerShell [Core] 7.0:

  • PowerShell 意外地没有
    打开命令的单个对象输出-OutVariable
    :也就是说,即使一个命令只输出一个对象,该对象也会意外地存储为一个数组(数组list) 在其名称被传递给公共参数 -OutVariable 参数(或其别名,-ov)的目标变量中.

  • PowerShell unexpectedly does not unwrap a command's single-object output with
    -OutVariable
    : That is, even if a command outputs only one object, that object is unexpectedly stored as an array (array list) in the target variable whose name is passed to the common parameter -OutVariable parameter (or its alias, -ov).

另外,存储在目标变量中的值的类型总是[System.Collections.ArrayList],一个数组list,这与通过正则赋值在变量中捕获多对象输出不一致,导致 System.Object[],一个常规数组.

Additionally, the type of the value stored in the target variable is always [System.Collections.ArrayList], an array list, which is inconsistent with capturing multi-object output in a variable by regular assignment, which results in System.Object[], a regular array.

请参阅此 GitHub 问题.

这里有一个简洁的测试来说明问题:

Here's a concise test that demonstrates the problem:

$null = Get-Date -OutVariable ov; $ov.GetType().FullName

从上面列出的版本开始,输出:

As of the versions listed above, this outputs:

System.Collections.ArrayList

即使输出(概念上)只是一个 single [datetime] ([System.DateTime]) 实例.

even though the output is (conceptually) only a single [datetime] ([System.DateTime]) instance.

将此与常规赋值进行对比:$v = Get-Date;$v.GetType().FullName 产生 System.DateTime.

Contrast this with a regular assignment: $v = Get-Date; $v.GetType().FullName yields System.DateTime.

解决方法 是将输出变量引用包装在子表达式运算符 ($(...)) 中,这样可以为您提供与通过常规输出(成功)流相同的输出:

The workaround is to wrap the output-variable reference in the subexpression operator ($(...)), which gives you the same output as via the regular output (success) stream:

  • 解包单元素集合(仅输出元素本身)
  • 多元素集合作为 System.Object[] 数组返回.
  • a single-element collection is unwrapped (only outputs the element itself)
  • a multi-element collection is returned as a System.Object[] array.
# Note the $(...) around $ov
PS> $null = Get-Date -OutVariable ov; $($ov).GetType().FullName
System.DateTime  # 1-element System.Collections.ArrayList unwrapped

当然,如果您只需要一个输出对象,您也可以使用 $ov[0].

Of course, you could also use $ov[0] if you only ever expect a single output object.

证明该解决方法对多元素输出也有效:

A demonstration that the workaround is effective with multi-element output too:

PS> $null = Get-ChildItem / -OutVariable ov; $($ov).GetType().FullName
System.Object[] # multi-element System.Collections.ArrayList converted to array

这篇关于使用 OutVariable 创建 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆