[System.Collections.Generic.List[string]] 作为返回值 [英] [System.Collections.Generic.List[string]] as return value
问题描述
我需要/想要从一个函数返回一个 [System.Collections.Generic.List[string]]
,但它被一个 System.Object[] 覆盖
我有这个
function TestReturn {$returnList = 新对象 System.Collections.Generic.List[string]$returnList.Add('测试,一,二')返回 ,@($returnList)}$testList = 测试返回$testList.GetType().FullName
将它作为 System.Object[]
返回,如果我将返回行更改为
return [System.Collections.Generic.List[string]]$returnList
或
return [System.Collections.Generic.List[string]]@($returnList)
当列表中有一项时返回一个[System.String]
,如果有多个项时返回一个System.Object[]
,在两种情况.列表有什么奇怪的地方不能用作返回值吗?
现在,奇怪的是(我认为)如果我像这样键入接收值的变量,它确实有效.
[System.Collections.Generic.List[string]]$testList = TestReturn
但这似乎是某种奇怪的强制转换,其他数据类型不会发生这种情况.
如果删除数组子表达式 @(...)
并在前面加上逗号.以下代码似乎有效:
function TestReturn {$returnList = 新对象 System.Collections.Generic.List[string]$returnList.Add('测试,一,二')返回 , $returnList}$testList = 测试返回$testList.GetType().FullName
注意:从技术上讲,这会导致 [Object[]]
返回带有 [System.Collections.Generic.List[string]]
类型的单个元素.但同样由于隐式展开,它会欺骗 PowerShell 根据需要进行输入.
稍后,语法 [Type]$Var
type 约束变量.它基本上锁定了该变量的类型.因此,对 .GetType()
的后续调用将返回该类型.
这些问题是由于 PowerShell 如何在输出时隐式展开数组.典型的解决方案,在某种程度上取决于类型,是在返回之前使用 ,
或确保调用端的数组,通过类型约束您的问题中显示的变量,或者包装或强制转换回报本身.后者可能类似于:
$testList = [System.Collections.Generic.List[string]]TestReturn$testList.GetType().FullName
为了确保可以在标量返回时使用数组,并且假设您没有在 return 语句之前使用 ,
,您可以在调用端使用数组子表达式:
$testList = @( TestReturn )$testList.GetType().FullName
我相信这个答案处理了类似的问题>
I have a need/want to return a [System.Collections.Generic.List[string]]
from a function, but it is being covered into a System.Object[]
I have this
function TestReturn {
$returnList = New-Object System.Collections.Generic.List[string]
$returnList.Add('Testing, one, two')
return ,@($returnList)
}
$testList = TestReturn
$testList.GetType().FullName
which returns it as a System.Object[]
, and if I change the return line to
return [System.Collections.Generic.List[string]]$returnList
or
return [System.Collections.Generic.List[string]]@($returnList)
it returns a [System.String]
when there is one item in the list, and a System.Object[]
if there is more than one item, in both cases. Is there something odd with a list that it can't be used as a return value?
Now, oddly (I think) it DOES work if I type the variable that receives the value, like this.
[System.Collections.Generic.List[string]]$testList = TestReturn
But that seems like some weird coercion and it doesn't happen with other data types.
If you remove the array subexpression @(...)
and just precede with a comma. The below code seems to work:
function TestReturn {
$returnList = New-Object System.Collections.Generic.List[string]
$returnList.Add('Testing, one, two')
return , $returnList
}
$testList = TestReturn
$testList.GetType().FullName
Note: technically this causes the return of [Object[]]
with a single element that's of type [System.Collections.Generic.List[string]]
. But again because of the implicit unrolling it sort of tricks PowerShell into typing as desired.
On your later point, the syntax [Type]$Var
type constrains the variable. It's basically locking in the type for that variable. As such subsequent calls to .GetType()
will return that type.
These issues are due to how PowerShell implicitly unrolls arrays on output. The typical solution, somewhat depending on the typing, is to precede the return with a ,
or ensure the array on the call side, either by type constraining the variable as shown in your questions, or wrapping or casting the return itself. The latter might look something like:
$testList = [System.Collections.Generic.List[string]]TestReturn
$testList.GetType().FullName
To ensure an array when a scalar return is possible and assuming you haven't preceded the return statement with ,
, you can use the array subexpression on the call side:
$testList = @( TestReturn )
$testList.GetType().FullName
I believe this answer deals with a similar issue
这篇关于[System.Collections.Generic.List[string]] 作为返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!