Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-如何确保参数属于特定类型(ArrayList)?_Powershell_Validation_Arraylist_Parameters - Fatal编程技术网

Powershell-如何确保参数属于特定类型(ArrayList)?

Powershell-如何确保参数属于特定类型(ArrayList)?,powershell,validation,arraylist,parameters,Powershell,Validation,Arraylist,Parameters,虽然这样做有效,但仅仅检查$InputArrayList的类型是ArrayList而不是$null似乎太多了 如何在param()部分中指定此选项?可能只是: function Global:Some-Function { param( [Parameter(Mandatory=$true)]$InputArrayList ) $debug = $true # Empty Validation if(($InputArrayList -

虽然这样做有效,但仅仅检查
$InputArrayList
的类型是
ArrayList
而不是
$null
似乎太多了

如何在
param()
部分中指定此选项?

可能只是:

function Global:Some-Function {
    param(
        [Parameter(Mandatory=$true)]$InputArrayList
    )

    $debug = $true

    # Empty Validation
    if(($InputArrayList -eq $null) -or ($InputArrayList.Count -le 0)) {
        if($debug) { Write-Host '$InputArrayList is null or count -le 0' }
        return 
    }

    # Verify the Input is an ArrayList
    if($InputArrayList.GetType().Name -ne 'ArrayList')
    {
        if($debug) { Write-Host '$InputArrayList is not of type "ArrayList"' -ForegroundColor Red }
        return
    }
  } # Function
也许只是:

function Global:Some-Function {
    param(
        [Parameter(Mandatory=$true)]$InputArrayList
    )

    $debug = $true

    # Empty Validation
    if(($InputArrayList -eq $null) -or ($InputArrayList.Count -le 0)) {
        if($debug) { Write-Host '$InputArrayList is null or count -le 0' }
        return 
    }

    # Verify the Input is an ArrayList
    if($InputArrayList.GetType().Name -ne 'ArrayList')
    {
        if($debug) { Write-Host '$InputArrayList is not of type "ArrayList"' -ForegroundColor Red }
        return
    }
  } # Function


@TessellatingEckler什么类是
ValidateNotNullOrEmpty()
来自?是PowerShell的“参数装饰”;它旨在强制传递的值为非null。
[System.Collections.ArrayList]$InputArrayList
由于param不起作用,它将传递即使是常规数组也不会抛出error@Avshalom我可以确认。@Avshalom它将根据需要工作-它不会通过常规数组,它将把它投给ArrayList。这可能是需要的,也可能不是需要的,但在函数内部,您得到的将是正确类型的集合,而不是其他任何东西。@tessellingheckler什么类是
ValidateNotNullOrEmpty()
from?是PowerShell的“参数装饰”;它旨在强制传递的值为非null。
[System.Collections.ArrayList]$InputArrayList
由于param不起作用,它将传递即使是常规数组也不会抛出error@Avshalom我可以确认。@Avshalom它将根据需要工作-它不会通过常规数组,它将把它投给ArrayList。这可能是你想要的,也可能不是你想要的,但是在函数中,你得到的是正确类型的集合,而不是其他任何东西!99.9%肯定这是答案,但我会给它一些时间看看其他人在这里说些什么,我会帮助…这就是答案。@EBGreen老实说,我很惊讶在
param()
部分没有内置的方法来验证类型。这似乎是函数中非常标准的操作call@EBGreen,这也不起作用,因为
$\
将检查内容而不是父类型,例如:
[ValidateScript({($\-is[System.Collections.ArrayList])}
将不起作用,但是如果ArrayList包含PSObject列表,这将起作用:
[ValidateScript({($\uis[psobject])})]
@Bill\u Stewart如果您尝试对非ArrayList的对象调用
.Add()
,该怎么办。。。甚至更糟糕的是,如果这个东西有
.Add()
方法,但它不是ArrayList,那么调试起来会很困难。没有办法确切知道接下来会发生什么,所以最好检查一下我觉得合适的答案!99.9%肯定这是答案,但我会给它一些时间看看其他人在这里说些什么,我会帮助…这就是答案。@EBGreen老实说,我很惊讶在
param()
部分没有内置的方法来验证类型。这似乎是函数中非常标准的操作call@EBGreen,这也不起作用,因为
$\
将检查内容而不是父类型,例如:
[ValidateScript({($\-is[System.Collections.ArrayList])}
将不起作用,但是如果ArrayList包含PSObject列表,这将起作用:
[ValidateScript({($\uis[psobject])})]
@Bill\u Stewart如果您尝试对非ArrayList的对象调用
.Add()
,该怎么办。。。甚至更糟糕的是,如果这个东西有
.Add()
方法,但它不是ArrayList,那么调试起来会很困难。没有办法确定会发生什么,所以最好检查一下我的感觉