PowerShell cmdlet 参数验证
我正在编写一个自定义 PowerShell cmdlet,我想知道哪种方法是验证参数的正确方法。
我认为这可以在属性集访问器中或在 Cmdlet 执行期间完成:
[Cmdlet(VerbsCommon.Add,"X")]
public class AddX : Cmdlet {
private string _name;
[Parameter(
Mandatory=false,
HelpMessage="The name of the X")]
public string name {
get {return _name;}
set {
// Should the parameter be validated in the set accessor?
if (_name.Contains(" ")) {
// call ThrowTerminatingError
}
_name = value;
}
}
protected override void ProcessRecord() {
// or in the ProcessRecord method?
if (_name.Contains(" ")) {
// call ThrowTerminatingError
}
}
}
哪种是“标准”方法?属性设置器、ProcessRecord 还是完全不同的东西?
I'm writing a custom PowerShell cmdlet, and I would like to know which is the proper way to validate a parameter.
I thought that this could be done either in the property set accessor or during Cmdlet execution:
[Cmdlet(VerbsCommon.Add,"X")]
public class AddX : Cmdlet {
private string _name;
[Parameter(
Mandatory=false,
HelpMessage="The name of the X")]
public string name {
get {return _name;}
set {
// Should the parameter be validated in the set accessor?
if (_name.Contains(" ")) {
// call ThrowTerminatingError
}
_name = value;
}
}
protected override void ProcessRecord() {
// or in the ProcessRecord method?
if (_name.Contains(" ")) {
// call ThrowTerminatingError
}
}
}
Which is the "standard" approach? Property setter, ProcessRecord or something completely different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果可能,最好由运行时通过指定
If possible, it's preferred that the parameters be validated by the runtime by specifying Validation Attributes on the parameter definition.