代码之家  ›  专栏  ›  技术社区  ›  MadBoy

powershell中的bool[]中是否可能有[nullable[bool]]

  •  4
  • MadBoy  · 技术社区  · 7 年前

    在powershell中的bool[]中可以有[nullable[bool]]吗?我尝试了不同的解决方案,但未能获得正确的$null,$true,$false参数?而且[CmdletBinding]似乎也改变了工作方式。

    enum BoolNull {
        null = $null
        true = $true
        false = $false
    }
    function Test-Array0 {
        param (
            [bool[]] $thisValue
        )
        if ($thisValue -eq $null) {
            Write-Output 'Null found'
        }
    }
    function Test-Array1 {
        param (
            [bool[]] $thisValue
        )
        foreach ($value in $thisValue) {
            if ($value -eq $null) {
                Write-Output 'Null found'
            }
        }
    }
    function Test-Array2 {
        [CmdletBinding()]
        param (
            [bool[]] $thisValue
        )
        if ($thisValue -eq $null) {
            Write-Output 'Null found'
        }
    }
    
    
    function Test-Array {
        [CmdletBinding()]
        param (
            [AllowEmptyCollection()] [AllowNull()][ValidateSet($null, $true, $false)] $thisValue
        )
        if ($thisValue -eq $null) {
            Write-Output 'Null found'
        }
    }
    
    # this works
    function Test-Test {
        [CmdletBinding()]
        param (
            [nullable[bool]] $thisValue
        )
        if ($thisValue -eq $null) {
            Write-Output 'Null found'
        }
    }
    
    function Test-Array5 {
        param (
            [boolnull[]] $thisValue
        )
        foreach ($value in $thisValue) {
            if ($value -eq 'null') {
                Write-Output 'Null found'
            }
        }
    }
    
    Test-Array0 -thisValue $null #this works
    Test-Array -thisValue $null # this doesn't work
    Test-Array -thisValue $null, $null, $true # this doesn't work
    Test-Array1 -thisValue $null
    Test-Array2 -thisValue $null # this
    Test-Test -thisValue $null # this works 
    Test-Array5 -thisValue null, true, null # this works but is completely useless
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  3
  •   Maximilian Burszley    7 年前

    这是bool类型的限制。当您严格键入参数时,它只能 $true ,请 $false 我是说, 0 ,和 1 是的。为了达到你想要的,你可以使用 [ValidateSet] 属性:

    [CmdletBinding()]
    param(
        [Parameter(Position = 0, Mandatory)]
        [ValidateSet($null, $true, $false)]
        [object] $ThisValue
    )
    

    顺便说一句,powershell曾经有一个bug(可能仍然存在),其中比较 $null 右边的语句将导致不返回任何内容,导致语句中出现逻辑错误,因此最好在左边进行比较:

    if ($null -eq $ThisValue) {
    

    在测试了您的示例之后,我无法复制您的问题,但是:

    function Test-Nullable {
        [CmdletBinding()]
        param(
            [nullable[bool]] $Value
        )
    
        if ($null -eq $Value) {
            'Yes'
        } else {
            $Value
        }
    }
    

    以及数组格式:

    function Test-Nullable {
        [CmdletBinding()]
        param(
            [nullable[bool][]] $Value
        )
    
        foreach ($bool in $Value) {
            if ($null -eq $bool) {
                'Yes'
            } else {
                $bool
            }
        }
    }
    
    Test-Nullable 5, 3, $null, $true, $false, 0
    True
    True
    Yes
    True
    False
    False
    
    推荐文章