我正在研究一个比较两个对象的函数,以便检测它们是否相同。但是我希望它也可以与其他类型一起使用,例如字符串或整数。
C ++允许您使用相同的名称声明不同的函数,以不同的方式处理具有不同输入类型的函数调用。我知道参数集的存在,但是据我所知,用户是必须指定他正在使用的参数集的人。
我正在尝试做这样的事情
function Compare-Objects{
Param(
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName = "Hashtables")]
[ValidateNotNullOrEmpty()]
[Hashtable]$Item1Hash,
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName = "Integers")]
[ValidateNotNullOrEmpty()]
[int]$Item1int,
[Parameter(Mandatory=$true,
Position=1,
ParameterSetName = "Hashtables")]
[ValidateNotNullOrEmpty()]
[Hashtable]$Item2Hash,
[Parameter(Mandatory=$true,
Position=1,
ParameterSetName = "Integers")]
[ValidateNotNullOrEmpty()]
[Hashtable]$Item2Int
)
if($PSCmdlet.ParameterSetNamePositionv -match "Integers"){ Return ($Item1Int -eq $Item2Int)}
else{
#do some other stuff with $Item1Hash and $Item2Hash
}
}
如果我也可以命名相同的变量,那么加分(因此$Item1Hash
和$Item1Int
都变成了$Item1
,并且分配了适当的类型)
答案 0 :(得分:1)
Jeff Zeitlin在其评论中指出:
用户不必明确指定参数集-PowerShell 从调用时传递的特定参数组合(或不存在参数)中推断适用的参数集。< / p>
推论基于参数数据类型,参数是否按位置传递 (不带参数名称)以及哪些参数标记为强制性< / em>,并且生效的参数集的名称将反映在调用的(advanced) script / function中的$PSCmdlet.ParameterSetName
中。
推断适用参数集的能力类似于类似C语言的自动重载解析。
虽然任何给定参数都可以参与多个参数集-并且实际上默认是所有全部的一部分-从根本上讲,您不能使用< em>名称相同,但不同的数据类型 。
如果您想要这样的“多态”参数,则必须实现自己的逻辑,该逻辑不依赖于参数集:
function Compare-Objects {
[CmdletBinding(PositionalBinding=$false)]
Param(
[Parameter(Mandatory, Position=0)]
[ValidateNotNullOrEmpty()]
# Define as [object] to initially accept any value; specific types
# are later enforced inside the function body.
[object] $Item1
,
[Parameter(Mandatory, Position=1)]
[ValidateNotNullOrEmpty()]
[object] $Item2
)
# Ensure that a supported type was passed.
if ($Item1.GetType() -notin [int], [hashtable]) { Throw "Unsupported argument type." }
# Ensure that both arguments have the same type.
if ($Item1.GetType() -ne $Item2.GetType()) { Throw "Inconsistent argument types." }
if ($Item1 -is [int]) {
"[int] arguments given."
}
else {
"[hashtable] arguments given."
}
}
但是,如果不需要使用相同参数名称,并且您对使用不同数据类型的 positional 调用感到满意,参数集可以提供帮助,如以下简化示例所示:
function Foo {
[CmdletBinding()]
param(
[Parameter(ParameterSetName='int', Position=0)]
[int] $ItemInt
,
[Parameter(ParameterSetName='hash', Position=0)]
[hashtable] $ItemHash
)
"Parameter set chosen: $($PSCmdlet.ParameterSetName)"
}
# Call the function first with an [int], then with a [hashtable], positionally.
10, @{ foo = 1 } | ForEach-Object { Foo $_ }
上面给出了以下内容,表明参数数据类型自动选择了适当的参数集:
Parameter set chosen: int
Parameter set chosen: hash