Zmiany PowerShell zwracają typ obiektu
Używam PowerShell v3 i Windows PowerShell ISE. Mam następującą funkcję, która działa dobrze:
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
}
Teraz będę tworzył kilka podobnych funkcji, więc chcę przerwać pierwsze 3 linie do nowej funkcji, dzięki czemu nie muszę ich kopiować i wklejać wszędzie, więc zrobiłem to:
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
}
Problem polega na tym, że gdy „return $ xmlNsManager” wykonuje następujący błąd:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Xml.XmlNamespaceManager".
Tak więc, mimo że jawnie rzutowałem moje zmienne $ xmlNsManager na typ System.Xml.XmlNamespaceManager, gdy zostanie zwrócony z funkcji Get-XmlNamespaceManager, PowerShell konwertuje go do tablicy obiektów.
Jeśli jawnie nie oddam wartości zwróconej z funkcji Get-XmlNamespaceManager do System.Xml.XmlNamespaceManager, następujący błąd jest generowany z funkcji .SelectSingleNode (), ponieważ niepoprawny typ danych jest przekazywany do drugiego parametru funkcji.
Cannot find an overload for "SelectSingleNode" and the argument count: "2".
Z jakiegoś powodu PowerShell nie utrzymuje typu danych zmiennej zwrotnej. Naprawdę chciałbym, żeby to działało z funkcji, dzięki czemu nie muszę kopiować i wklejać tych 3 linii w całym miejscu. Wszelkie sugestie są mile widziane. Dzięki.