如何将 SwitchParameter 添加到属性集合?
How to add a SwitchParameter to an attribute collection?
我有一个这样定义的属性集合:
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
虽然我能够添加属性和验证集选项,但我无法将 SwitchParameter 添加到集合中。
$SwitchParameter = New-Object System.Management.Automation.SwitchParameter
$AttributeCollection.Add($SwitchParameter)
当我 运行 以上时,我得到以下错误:
Cannot find an overload for "Add" and the argument count: "1".
由于属性集合采用 System.Attribute
类型的参数,我想一定有不同的方法来添加 SwitchParameter,但我不确定如何。
您不必使用任何附加属性来使参数成为切换参数。您只需将其类型声明为 System.Management.Automation.SwitchParameter
或 switch
.
function f{
[CmdletBinding()]
param(
[string[]]$Names
)
dynamicparam{
$DynamicParams=New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
foreach($Name in $Names){
$Attributes=@(
New-Object Parameter -Property @{ParameterSetName="Set_$Name"}
)
$Param=New-Object System.Management.Automation.RuntimeDefinedParameter $Name,switch,$Attributes
$DynamicParams.Add($Name,$Param)
}
$DynamicParams
}
}
我有一个这样定义的属性集合:
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
虽然我能够添加属性和验证集选项,但我无法将 SwitchParameter 添加到集合中。
$SwitchParameter = New-Object System.Management.Automation.SwitchParameter
$AttributeCollection.Add($SwitchParameter)
当我 运行 以上时,我得到以下错误:
Cannot find an overload for "Add" and the argument count: "1".
由于属性集合采用 System.Attribute
类型的参数,我想一定有不同的方法来添加 SwitchParameter,但我不确定如何。
您不必使用任何附加属性来使参数成为切换参数。您只需将其类型声明为 System.Management.Automation.SwitchParameter
或 switch
.
function f{
[CmdletBinding()]
param(
[string[]]$Names
)
dynamicparam{
$DynamicParams=New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
foreach($Name in $Names){
$Attributes=@(
New-Object Parameter -Property @{ParameterSetName="Set_$Name"}
)
$Param=New-Object System.Management.Automation.RuntimeDefinedParameter $Name,switch,$Attributes
$DynamicParams.Add($Name,$Param)
}
$DynamicParams
}
}