PowerShell 转换从函数返回的值.如何避免这种情况? [英] PowerShell converts values returned from function. How to avoid this?

查看:28
本文介绍了PowerShell 转换从函数返回的值.如何避免这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试返回 List<T> 来自 PowerShell 函数,但得到以下之一:

I am trying to return List< T> from PowerShell function, but get one of:

  1. null - 空列表
  2. System.Int32 - 一个元素的列表
  3. System.Object[] - 用于包含更多元素的列表

代码:

function CreateClrList
{
    $list = New-Object "System.Collections.Generic.List``1[System.Int32]"
    $list.Add(3)
    $list
}

Write-Host (CreateClrList).GetType()

推荐答案

是的,powershell 会展开所有集合.一种解决方案是使用一元逗号返回包含真实集合的集合:

Yeah, powershell unrolls all collections. One solution is to return a collection containing the real collection, using the unary comma:

function CreateClrList
{
    $list = New-Object "System.Collections.Generic.List``1[System.Int32]"
    $list.Add(3)
    ,$list
}

这篇关于PowerShell 转换从函数返回的值.如何避免这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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