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
}
Проблема в том, что когдавернуть $ 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 () выдается следующая ошибка, поскольку в функцию передается неверный тип данныхс 2-й параметр.
Cannot find an overload for "SelectSingleNode" and the argument count: "2".
По какой-то причине PowerShell не поддерживает тип данных возвращаемой переменной. Я действительно хотел бы получить это работает из функции, чтобы я ненужно скопировать и вставить эти 3 строки повсюду. Любые предложения приветствуются. Благодарю.