【发布时间】:2015-06-25 20:42:25
【问题描述】:
这种行为很神秘!
考虑以下 PowerShell 脚本:
[Reflection.Assembly]::LoadFrom("Newtonsoft.Json.dll") | Out-Null
function ConvertFrom-JsonNet {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string] $Json
)
$O = [Newtonsoft.Json.Linq.JObject]::Parse($Json)
Write-Host $O.GetType().Name
return $O
}
Clear-Host
$Json = '{"test":"prop"}'
$O1 = ConvertFrom-JsonNet '{"test":"prop"}'
$O2 = [Newtonsoft.Json.Linq.JObject]::Parse($Json)
Write-Host $O1.GetType().Name
Write-Host $O2.GetType().Name
你希望输出是:
JObject
JObject
JObject
但事实并非如此!它是:
JObject
JProperty
JObject
这怎么可能? within函数JObject的对象的类型如何,但是传出函数后却是JProperty?
【问题讨论】:
-
JObject实现了几个接口;其中之一是IEnumerable。 PowerShell 是否检测到它实现了IEnumerable并在将对象放在管道上时扩展它?
标签: powershell powershell-4.0 powershell-cmdlet