PowerShell/GetEnumerator [英] PowerShell/GetEnumerator
本文介绍了PowerShell/GetEnumerator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的问题:
为什么下面的程序不能写入整个数组 $ lines
?它永远循环.我真的很困惑.
Why the program below does not write the whole array $lines
? It is looping forever. I am really confused..
function Get-Current ($enumerator) {
$line = $enumerator.Current
return $line
}
$lines = @('a', 'b', 'c')
$enumerator = $lines.GetEnumerator()
$enumerator.Reset()
while ($enumerator.MoveNext()) {
$line = Get-Current $enumerator
"$line"
}
推荐答案
似乎表达式:
$enumerator
展开整个枚举数.例如:
unwinds the entire enumerator. For e.g.:
PS C:\> $lines = @('a', 'b', 'c')
PS C:\> $enumerator = $lines.GetEnumerator()
PS C:\> $enumerator.Reset()
PS C:\> $enumerator
a
b
c
因此,当您尝试将表达式传递给函数时,它将变为无效.我能想到的唯一解决方法是像这样将其作为PSObject传递:
So, when you try to pass your expression to the function, it is rendered invalid. The only workaround I can think of is to pass it as a PSObject like so:
function Get-Current ($val) {
$val.Value.Current
}
$lines = @('a', 'b', 'c')
$enumerator = $lines.GetEnumerator()
$enumerator.Reset()
while ($enumerator.MoveNext()) {
$line = Get-Current $(get-variable -name enumerator)
$line
}
这篇关于PowerShell/GetEnumerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文