如果设置了数据类型,则参数值不能设置为null

时间:2020-12-31 17:08:22

I just finished typing out a long question, only to figure out the answer on my own right before I posted. However, it is still not a perfect solution and I am wondering if I could get an explanation why the following won't work.

我刚刚完成了一个很长的问题,只是为了在我发布之前自己找出答案。但是,它仍然不是一个完美的解决方案,我想知道我是否能得到解释为什么以下不起作用。

What I want to do is have an optional parameter, that if not set, will be null. I would like that parameter to be a string.

我想要做的是有一个可选参数,如果没有设置,将为null。我希望该参数是一个字符串。

So I would do something like this:

所以我会做这样的事情:

function myfunction {
    param([string]$x = $null)
    set-aduser -identity someuser -officephone $x
}

By doing that, it seems like $x can never be null, even if I explicitly set it later in the code. If I were to remove [string], it works fine. Just wondering why that would be. It's not a big deal, but I can't see why it wouldn't work.

通过这样做,似乎$ x永远不会为null,即使我稍后在代码中明确设置它。如果我要删除[string],它可以正常工作。只是想知道为什么会这样。这不是什么大问题,但我不明白为什么它不起作用。

1 个解决方案

#1


This is because you are casting the parameter value to a string:

这是因为您将参数值转换为字符串:

param([string]$x = $null)
      ^^^^^^^^

Casting $null to a string always returns an empty string:

将$ null转换为字符串始终返回空字符串:

PS > [string]$x = $null
PS > $x.GetType() 

IsPublic IsSerial Name                                     BaseType                
-------- -------- ----                                     --------                
True     True     String                                   System.Object           


PS > $x.Length
0
PS >

One way to get around this would be to remove the cast and check the type of the variable yourself using the -is operator:

解决这个问题的一种方法是删除强制转换并使用-is运算符自己检查变量的类型:

function myfunction {
    param($x)
    if ($x -is [string] -or $x -eq $null) {
        set-aduser -identity someuser -officephone $x
    }
}

You should then probably also output an error if the user passes an argument of the wrong type or else the function will apparently do nothing, which can be confusing.

如果用户传递了错误类型的参数,或者该函数显然什么都不做,那么您可能还应该输出错误,这可能会造成混淆。

Buy if you are already doing all this, you should ask yourself if it would not be better to just have Set-ADUser output the error for you. It would be more efficient, involve less code, and ultimately produce the same result.

如果你已经完成了所有这些,那么你应该问问自己,让Set-ADUser为你输出错误是不是更好。它会更高效,涉及更少的代码,并最终产生相同的结果。

#1


This is because you are casting the parameter value to a string:

这是因为您将参数值转换为字符串:

param([string]$x = $null)
      ^^^^^^^^

Casting $null to a string always returns an empty string:

将$ null转换为字符串始终返回空字符串:

PS > [string]$x = $null
PS > $x.GetType() 

IsPublic IsSerial Name                                     BaseType                
-------- -------- ----                                     --------                
True     True     String                                   System.Object           


PS > $x.Length
0
PS >

One way to get around this would be to remove the cast and check the type of the variable yourself using the -is operator:

解决这个问题的一种方法是删除强制转换并使用-is运算符自己检查变量的类型:

function myfunction {
    param($x)
    if ($x -is [string] -or $x -eq $null) {
        set-aduser -identity someuser -officephone $x
    }
}

You should then probably also output an error if the user passes an argument of the wrong type or else the function will apparently do nothing, which can be confusing.

如果用户传递了错误类型的参数,或者该函数显然什么都不做,那么您可能还应该输出错误,这可能会造成混淆。

Buy if you are already doing all this, you should ask yourself if it would not be better to just have Set-ADUser output the error for you. It would be more efficient, involve less code, and ultimately produce the same result.

如果你已经完成了所有这些,那么你应该问问自己,让Set-ADUser为你输出错误是不是更好。它会更高效,涉及更少的代码,并最终产生相同的结果。