自定义Validate类,继承ValidateEnumeratedArgumentsAttribute


PowerShell交流中心分类: Powershell基础自定义Validate类,继承ValidateEnumeratedArgumentsAttribute
0
nutix asked 6年 ago
Start your code here

class ValidateExcludeSetAttribute: System.Management.Automation.ValidateEnumeratedArgumentsAttribute{
[bool]$IgnoreCase
[Object[]]$ValidValues
ValidateExcludeSetAttribute([object[]]$ValidValues){
$this.ValidValues = $ValidValues;
}
[void]Validate([object]$arguments, [System.Management.Automation.EngineIntrinsics]$engineIntrinsics){
foreach($arg in $arguments){
$this.ValidateElement($arg)
}
}
[void]ValidateElement([object]$element){
if($element -in $this.ValidValues){
throw [System.Management.Automation.ValidationMetadataException]::new(
“The parameter must be in {$($($this.ValidValues) -join ‘ | ‘)}.”)
}
}
}
function abc{
Param(
[ValidateExcludeSetAttribute(‘fuck’,123,250)]
$TheValue
)
}
abc 5
abc 123
abc ‘fuck’
#1.上面的代码为什么无法实现如同ValidateSet那样的参数验证???
#2.对ValidateEnumeratedArgumentsAttribute的继承,怎么实现向构造函数传递值表如ValidateSet,C#中是通过params关键字实现的,PowerShell中怎么实现?