PowerShell和$null作为方法参数

本文关键字:方法 参数 null PowerShell | 更新日期: 2023-09-27 18:13:04

PowerShell的$null等同于。net的null吗?我有一个场景,其中我必须调用一个。net方法从PowerShell和传递null作为方法参数。到目前为止,我在方法调用上得到了一些与程序集加载相关的错误。我想知道$null是否是罪魁祸首。另外,如果你知道如何从PowerShell中调用带有null参数的。net方法,请分享。

方法签名:

void SetUp(IKernel kernel, Action<IBindingInSyntax<object>> obj)

谢谢

PowerShell和$null作为方法参数

PowerShell在函数调用中将$null转换为。net的null。

举例证明。

Add-Type -TypeDefinition @"
public static class Foo
{
    public static bool IsNull(object o)
    {
        return o == null;
    }
}
"@ -Language CSharp
[Foo]::IsNull($null)
[Foo]::IsNull("string")
输出:

True
False

$null封装了对对象的null引用,难怪它只是工作,即使它们是两个不同的东西。