PowerShell和$ null作为方法的参数参数、方法、PowerShell、null

2023-09-05 03:23:52 作者:不再依赖人海中的你

时的PowerShell中的 $空等同于.NET的?我有一种情况,其中,我必须调用从PowerShell中的.NET方法,并通过作为方法的参数。截至目前,我得到了一些在方法调用组件加载相关的错误。我想知道如果 $空是罪魁祸首。另外,请分享,如果你知道什么是PowerShell中调用.NET方法参数的正确方法。

Is PowerShell's $null equivalent to .NET's null? I have a scenario, wherein I have to invoke a .NET method from PowerShell and pass null as method parameter. As of now, I am getting some assembly loading related error on method invocation. I am wondering if $null is the culprit. Also, please share if you know what is the correct way of invoking .NET method with null parameter from PowerShell.

方法签名:

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

感谢

推荐答案

PowerShell的转换是$空到.NET的空就好了函数调用。

PowerShell converts it's $null to .NET's null just fine in function calls.

证明以身作则。

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 wraps a null reference to an object, and no wonder it just works, even they are two different things.