【发布时间】:2011-02-22 21:08:01
【问题描述】:
我注意到大多数 Powershell 高级函数声明映射到特定 .Net 类型的标准数据类型(字符串、int、bool、xml、数组、哈希表等)的参数。
如何使用另一种 .Net 数据类型声明高级函数参数?例如,这是一个人为的例子:
function Do-Something
{
[CmdletBinding()]
Param(
[System.Xml.XPathNodeList] $nodeList
)
Begin {}
Process
{
Foreach ($node in $nodeList)
{
Write-Host $node
}
}
End {}
}
# Prepare to call the function:
$xml = [xml](get-content .\employee.xml)
$nodeList = $xml.SelectNodes("//age")
# Call the function passing an XPathNodeList:
do-something $nodeList
调用此函数会导致以下运行时错误:
Unable to find type [System.Xml.XPathNodeList]: make sure that the assembly
containing this type is loaded.
这可以用 LoadWithPartialName() 来完成吗?如何?
假设这是可能的,这里有一个辅助问题:以这种方式使用非标准类型会违反“最佳实践”吗?
【问题讨论】:
标签: function powershell types parameters