- 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('**',123,250)]
- $TheValue
- )
- }
- abc 5
- abc 123
- abc '**'
- #1.上面的代码为什么无法实现如同ValidateSet那样的参数验证???
- #2.对ValidateEnumeratedArgumentsAttribute的继承,怎么实现向构造函数传递值表如ValidateSet,C#中是通过params关键字实现的,PowerShell中怎么实现?
复制代码
|