PowerShell $null is not null any more when calling into C# code

后端 未结 1 1079
情歌与酒
情歌与酒 2021-01-19 01:42

In PowerShell one can define C# code and execute it. Passing in $null into the following simplest function shows that not null gets passed into the function



        
1条回答
  •  别那么骄傲
    2021-01-19 02:11

    Evidently the solution is "don't pass $null to a [String] parameter if you want a $null [String]". There is a special [NullString] class that should be used instead.

    PS > [foo]::fooo($Null)
    Not Null. Is ''
    PS > [foo]::fooo([NullString]::Value)
    PS >
    

    I can't find any mentions of the need to use [NullString] in the PowerShell documentation, nor does the source code for the NullString class include any comments explaining its purpose.

    0 讨论(0)
提交回复
热议问题