函数 – PowerShell更改返回对象的类型

我正在使用PowerShell v3和Windows PowerShell ISE.我有以下功能可以正常工作:

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)

    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the node, then return it. Returns $null if the node was not found.
    $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
    return $node
}

现在,我将会创建一些类似的功能,所以我想把前3行打破一个新的功能,所以我不必复制粘贴到任何地方,所以我做到了这一点:

function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)
    return $xmlNsManager
}

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    [System.Xml.XmlNamespaceManager]$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the node, then return it. Returns $null if the node was not found.
    $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
    return $node
}

问题是当“return $xmlNsManager”执行时抛出以下错误:

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Xml.XmlNamespaceManager".

所以即使我明确地将我的$xmlNsManager变量转换为System.Xml.XmlNamespaceManager类型,当从Get-XmlNamespaceManager函数返回时,PowerShell将其转换为Object数组.

如果我没有将Get-XmlNamespaceManager函数返回的值显式转换为System.Xml.XmlNamespaceManager,则会从.SelectSingleNode()函数抛出以下错误,因为错误的数据类型被传递到函数的第二个参数中.

Cannot find an overload for "SelectSingleNode" and the argument count: "2".

所以由于某种原因,PowerShell不会保留返回变量的数据类型.我真的很想从一个功能中得到这个工作,所以我不必在这个地方复制粘贴这3行.任何建议是赞赏.谢谢.

最佳答案
PowerShell正在将命名空间管理器对象转换为字符串数组.

我认为它与PowerShell的“展开”集合的性质有关.我认为PowerShell会为实现IEnumerable(具有GetEnumerator方法)的任何类型执行此操作.

作为一个工作,您可以使用逗号伎俩来防止此行为,并将对象作为整个集合发送.

function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
{
    ...
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)
    return ,$xmlNsManager
}

转载注明原文:函数 – PowerShell更改返回对象的类型 - 代码日志