标题: [问题求助] 自定义Validate类,继承ValidateEnumeratedArgumentsAttribute [打印本页]
作者: wrove 时间: 2018-4-10 18:57 标题: 自定义Validate类,继承ValidateEnumeratedArgumentsAttribute
- 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中怎么实现?
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |