PowerShell和$ null作为方法的参数 [英] PowerShell and $null as method parameter

查看:1362
本文介绍了PowerShell和$ null作为方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时的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.

这篇关于PowerShell和$ null作为方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆