简体   繁体   English

C# 二进制 PowerShell 模块中的 ValidateScript ParameterAttribute

[英]ValidateScript ParameterAttribute in C# Binary PowerShell Module

I have recently begun experimenting with Binary PowerShell Programming in C#, and I am having some trouble with ParameterValidationAttributes, the ValidateScript Attribute mostly.我最近开始在 C# 中尝试二进制 PowerShell 编程,但我在 ParameterValidationAttributes(主要是 ValidateScript 属性)方面遇到了一些问题。 Basically, i want to create a Param named "ComputerName" and validate the computer is online at that time.基本上,我想创建一个名为“ComputerName”的参数并验证当时计算机是否在线。 It was easy in PowerShell:在 PowerShell 中很容易:

    [Parameter(ValueFromPipeLine = $true)]
    [ValidateScript({ if (Test-Connection -ComputerName $_ -Quiet -Count 1) { $true } else { throw "Unable to connect to $_." }})]
    [String]
    $ComputerName = $env:COMPUTERNAME,

But i cannot figure out how to replicate that in C#.但我不知道如何在 C# 中复制它。 the ValidateScript attribute takes a ScriptBlock object http://msdn.microsoft.com/en-us/library/system.management.automation.scriptblock(v=vs.85).aspx im just not sure how to create that in C#, and i cannot really find any examples. ValidateScript 属性采用 ScriptBlock 对象http://msdn.microsoft.com/en-us/library/system.management.automation.scriptblock(v=vs.85).aspx我只是不确定如何在 C# 中创建它,我真的找不到任何例子。

[Parameter(ValueFromPipeline = true)]
[ValidateScript(//Code Here//)]
public string ComputerName { get; set; }

C# is very new to me, so i appologize if this is a dumb question. C# 对我来说很陌生,所以如果这是一个愚蠢的问题,我深表歉意。 here is a link the ValidateScript Attribute Class: http://msdn.microsoft.com/en-us/library/system.management.automation.validatescriptattribute(v=vs.85).aspx这是 ValidateScript 属性类的链接: http : //msdn.microsoft.com/en-us/library/system.management.automation.validatescriptattribute(v=vs.85).aspx

It is not possible in C#, since .NET only allow compile time constants, typeof expressions and array creation expressions for attribute parameters and only constant available for reference types other then string is null .这在 C# 中是不可能的,因为 .NET 只允许编译时常量、 typeof表达式和数组创建表达式用于属性参数,并且只有常量可用于引用类型,而不是string is null Instead you should derive from ValidateArgumentsAttribute and override Validate to perform validation:相反,您应该从ValidateArgumentsAttribute派生并覆盖Validate以执行验证:

class ValidateCustomAttribute:ValidateArgumentsAttribute {
    protected override void Validate(object arguments,EngineIntrinsics engineIntrinsics) {
        //Custom validation code
    }
}

Just to expand on user4003407's answer above with a more complete example.只是通过一个更完整的示例来扩展上面 user4003407 的答案。

Derive a new validator from ValidateArgumentsAttribute and override Validate to perform validation.从 ValidateArgumentsAttribute 派生一个新的验证器并覆盖 Validate 以执行验证。 Validate is void so you really just get to throw exceptions in cases you choose.验证是无效的,所以你真的只能在你选择的情况下抛出异常。

Kevin Marquette has an excellent article but it's in powershell. Kevin Marquette 有一篇很棒的文章,但它是在 powershell 中的。 Here is ac# example:这是 ac# 示例:

[Cmdlet(VerbsCommon.Get, "ExampleCommand")]
public class GetSolarLunarName : PSCmdlet
{   
    [Parameter(Position = 0, ValueFromPipeline = true, Mandatory = true)]
    [ValidateDateTime()]
    public DateTime UtcDateTime { get; set; }

    protected override void ProcessRecord()
    {

        var ExampleOutput = //Your code
        this.WriteObject(ExampleOutput);
        base.EndProcessing();
    }
}

class ValidateDateTime:ValidateArgumentsAttribute {
protected override void Validate(object arguments,EngineIntrinsics engineIntrinsics) {
    var date = (DateTime)arguments;
    if( date.Year < 1700 || date.Year > 2082){
        throw new ArgumentOutOfRangeException();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM