函数返回Object Array而不是ArrayList

时间:2018-06-14 03:48:51

标签: powershell

昨天问这个问题......

Returning ArrayList from Function/Script

这个问题的目的是帮助我完成这个功能......

function Initialize-ThreadPool {
    Param(
        [Parameter(Mandatory=$true,Position=1)][scriptblock] $ScriptBlock
      , [Parameter(Mandatory=$false,Position=2)][Hashtable] $Parameters
      , [Parameter(Mandatory=$true,Position=3)][int64] $Occurrences
      , [Parameter(Mandatory=$false,Position=4)][int] $Throttle = 3
    )

    $threads_all = New-Object 'System.Collections.ArrayList'

    [runspacefactory]::CreateRunspacePool() | Out-Null

    $session_state = `
        [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()

    $runspace_pool = [runspacefactory]::CreateRunspacePool(1, $Throttle)
    $runspace_pool.ApartmentState = "STA"
    $runspace_pool.Open()

    for ($iterator = 1; $iterator -le $Occurrences; $iterator++) {

        $session = [Powershell]::Create()
        $session.AddScript($ScriptBlock)
        if ( $Parameters ) {
            $session.AddParameters($Parameters) | Out-Null
        }
        $session.RunspacePool = $runspace_pool

        $thread_created = @{
            'session' = $session
            'thread_handle' = $session.BeginInvoke()
        }

        $threads_all.Add($thread_created) | Out-Null
    }

    Write-Output -NoEnumerate $threads_all
}

当我调用这个函数时,我得到一个对象数组而不是一个ArrayList,我不明白为什么。我在这里或多或少做同样的事情......

function f {
    [CmdletBinding()]Param()
    Write-Verbose 'f: Start'
    $t = New-Object 'System.Collections.ArrayList'
    Write-Verbose $t.GetType().Name
    Write-Output -NoEnumerate $t
}

$things = New-Object 'System.Collections.ArrayList'
$things.GetType().Name
$things = f -verbose
$things.GetType().Name

而且,在这种情况下,我得到一个ArrayList。 WTF?

0 个答案:

没有答案