相关疑难解决方法(0)

为什么Powershell的"return"关键字会导致类型错误?

$xml = [xml] '<node>foo</node>'
function foo2 { return "foo2" }

# all of these fail with the message:
# **Cannot set "foo" because only strings can be used as values to set XmlNode properties.**
$xml.node = foo2
$xml.node = foo2 -as [string]  # because of this issue[1]
$xml.node = (foo2)  

# these work
$xml.node = (foo2).tostring()
$xml.node = (foo2) -as [string]
$xml.node = [string] (foo2)

# yet, these two statements return the same value
(foo2).gettype()
(foo2).tostring().gettype()
Run Code Online (Sandbox Code Playgroud)

1:PowerShell函数返回行为

powershell

4
推荐指数
1
解决办法
1779
查看次数

标签 统计

powershell ×1