指定其他参数时不调用GetDynamicParameters()

时间:2015-10-06 21:23:03

标签: c# powershell dynamic parameters cmdlets

我在C#中开发了一个定义dynamic parameter Get-Foo的PowerShell cmdlet Bar,只有在指定了switch参数MySwitch时才会显示该cmdlet MyString。它还有其他参数MyIntMyBoolBar

当我运行cmdlet时,当我指定切换参数MySwitch以及MyStringMyInt时,动态参数MyBool会显示在制表符完成中。不是在我使用namespace MyCompany.Cmdlets { [Cmdlet(VerbsCommon.Get, "Foo")] public class GetFoo : PSCmdlet, IDynamicParameters { [Parameter] public string MyString { get; set; } [Parameter] public int MyInt { get; set; } [Parameter] public bool MyBool { get; set; } [Parameter] public SwitchParameter MySwitch { get; set; } public object GetDynamicParameters() { if (MySwitch.IsPresent) { context = new FooParameters(); return context; } return null; } private FooParameters context; protected override void ProcessRecord() {} } public class FooParameters { [Parameter] public string Bar { get; set; } } } (或任何其他参数类型)时。

以下是代码:

MyBool

我做错了什么?如何在指定MyObject参数(或library("statmod") x <- sort(rinvgauss(90,0.000471176,0.0000191925)) y <- sort(rinvgauss(90,0.000732085,0.000002982015)) z <- sort(rinvgauss(180,0.000286672,0.00000116771)) den <- pinvgauss(x,0.000471176,0.0000191925) dens <- pinvgauss(y,0.000732085,0.000002982015) densi <- pinvgauss(z,0.000286672,0.00000116771) rel <- 1-den reli <- 1-dens relia <- 1-densi plot(x,rel, xlim=c(0,0.002), col="red", type="l") lines(y,reli, col="blue") lines(z,relia, col="black") 参数)时显示动态参数?

1 个答案:

答案 0 :(得分:1)

当使用struct&#34; SwitchParameter&#34;时,我会像对待任何其他布尔值一样对待它,例如:

if (MySwitchParameter)
{ 
   // la la la other stuff
}

GetDynamicParameters很棘手,但是,就Return而言,你有两个选择 值:返回表示参数的对象 OR 返回a RuntimeDefinedParameterDictionary对象。想想RuntimeDefinedParameterDictionary 作为&#34; CodeDom-Like&#34;表达你的参数的方式。

以下是对Get-Foo Cmdlet的重写,重点是动态参数:

[Cmdlet(VerbsCommon.Get, "Foo",
    SupportsShouldProcess = true
    )]
public class GetFooCommand : PSCmdlet, IDynamicParameters
{
    // First things first: Lets try making a dictionary for our return 
    // value in our implementation of IDynamicParameter's 
    // GetDynamicParameters() method
    private RuntimeDefinedParameterDictionary DynamicParameters;

    [Parameter]
    public string MyString { get; set; }

    [Parameter]
    public int MyInt { get; set; }

    [Parameter]
    // Booleans are not as fun as SwitchParameters, IMHO
    public bool MyBool { get; set; }


    // Remember that SwitchParameter is really just a boolean (sort of), so 
    // it will default to 'false'
    [Parameter]
    public SwitchParameter MySwitch { get; set; }

    public object GetDynamicParameters()
    {
        // You only want this to run when the switch is flipped,
        // so try it this way and see if it works for you
        if (MySwitch)
        {
            // Create the dictionary. We will return this at the end because
            // **** spoiler alert **** it is an object :)
            var runtimeParameterDictionary = new RuntimeDefinedParameterDictionary();


            // Lets make that parameter now. Your example doesn't specify any 
            // additional parameter attributes beyond the required "ParameterAttribute", 
            // so here we create an empty Attribute Collection
            var runtimeParameter = 
            new RuntimeDefinedParameter("Bar", typeof (string), new Collection<Attribute>());

            // With a new Parameter, lets add it to our dictionary. 
            runtimeParameterDictionary.Add("Bar", runtimeParameter);

            // Because we created a field for our dictionary way up top, we can assign it like I
            // illustrate below. This will enable easy, predictable results when we seek to 
            // extract the values from the dictionary at runtime.
            DynamicParameters = runtimeParameterDictionary;


            // Note: You can add as many parameters as you want this way, and that Is 
            // why I recommend it to you now. Coding the parameters as classes outside of the 
            // Cmdlet, and to some extent as embedded Classes,
            // within the Cmdlet Never really worked for me, so I feel your pain/frustration.

            return runtimeParameterDictionary;
        }

        return null; // Guess the user doesn't want that Bar afterall;
    }

    protected override void ProcessRecord()
    {
        // We obviously want to sequester everything we are doing with the dynamic
        // parameters so a good-old-fashioned if.. then.. else... will suffice.
        if (MySwitch)
        {
            // Now we are here at the precipice of this DynamicParameter cmdlet.
            // Here is one way to get the value desired from the dynamic parameter.

            // Simply access it like a dictionary...
            var bar = DynamicParameters["Bar"].Value as string;

            // The reason we can do it this way is because we assigned our
            // beloved value to the local variable "DynmaicParameters"
            // in the GetDynamicParameters() method. We could care less about 
            // the return value, because if the 
            // switch is flipped, "DynamicParameters" will be our new best friend, and
            // it will have everything we need.

            WriteWarning("Your " + bar + " has been forwarded to an automatic voice messaging system ...");
        }


        WriteObject("Cheers was filmed before a live studio audience...");
        WriteObject(MyInvocation.BoundParameters);
    }
}