简体   繁体   English

布尔变量作为Object []返回

[英]Boolean variable gets returned as an Object[]

I have a function which is returning a boolean variable 我有一个函数返回一个布尔变量

function test ($param1, $param2)
{
  [boolean]$match=$false;

  <# function logic #>

  return $match
}

when I try and catch the function call in a variable $retByFunct=testing $param1 $param 2 当我尝试在变量$ retByFunct = testing $ param1 $ param 2中捕获函数调用时

I am getting $retByFunct as an Object Array. 我将$ retByFunct作为对象数组。 If I try and force the $retByFunct to be a boolean variable ie [boolean] $retByFunct=testing $param1 $param 2, I get the following error. 如果我尝试强制$ retByFunct为布尔变量,即[boolean] $ retByFunct = testing $ param1 $ param 2,则会收到以下错误。

Cannot convert value "System.Object[]" to type System.Boolean 无法将值“ System.Object []”转换为System.Boolean类型

I checked out $match.GetType() just before returning it. 我刚刚返回$ match.GetType()。 The console says its a boolean, so am confused as to why after function call its getting converted to an Object Array. 控制台说它是一个布尔值,所以对于为什么在函数调用后将其转换为对象数组感到困惑。

I am aware this happens for some collection objects and there is a work around for that, but how do I handle a case for a variable? 我知道某些收集对象会发生这种情况,并且可以解决该问题,但是如何处理变量的大小写呢?

As @mjolinor said, you need to show the rest of your code. 正如@mjolinor所说,您需要显示其余代码。 I suspect it's generating output on the success output stream somewhere. 我怀疑它会在某个地方的成功输出流上生成输出。 PowerShell functions return all output on that stream, not just the argument of the return keyword. PowerShell函数返回该流上的所有输出,而不仅仅是return关键字的参数。 From the documentation : 文档中

In PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword. 在PowerShell中,即使没有包含Return关键字的语句,每个语句的结果也将作为输出返回。 Languages like C or C# return only the value or values that are specified by the return keyword. 诸如C或C#之类的语言仅return关键字指定的一个或多个值。

There's no difference between 两者之间没有区别

function Foo {
  'foo'
}

or 要么

function Foo {
  'foo'
  return
}

or 要么

function Foo {
  return 'foo'
}

Each of the above functions returns the string foo when called. 上面的每个函数在调用时都返回字符串foo

Additional output causes the returned value to be an array, eg: 附加输出使返回值成为数组,例如:

function Foo {
  $v = 'bar'
  Write-Output 'foo'
  return $v
}

This function returns an array 'foo', 'bar' . 此函数返回数组'foo', 'bar'

You can suppress undesired output by redirecting it to $null : 您可以通过将其重定向到$null来抑制不想要的输出:

function Foo {
  $v = 'bar'
  Write-Output 'foo' | Out-Null
  Write-Output 'foo' >$null
  return $v
}

or by capturing it in a variable: 或通过将其捕获到变量中:

function Foo {
  $v = 'bar'
  $captured = Write-Output 'foo'
  return $v
}

return something ## is not the only thing that PowerShell returns. 返回某些信息##不是PowerShell返回的唯一内容。 This is one of the gotchas of PowerShell, if you will. 如果愿意,这是PowerShell的陷阱之一。 All output on the success output stream will also be returned. 成功输出流上的所有输出也将被返回。 that's why you have an array of objects returning from your function. 这就是为什么您要从函数返回一系列对象的原因。

function test {
  "hello"
  return "world"
}

$mytest=test
$mytest 

try the code above... you will not get just "world" but "hello" "world" 尝试上面的代码...您将不仅获得“世界”,还获得“你好”,“世界”

$mytest.count
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM