deactivate parameters if a certain other parameter is used

问题描述 投票:0回答:1

I'm looking for a solution as follow and hope for your support:

In my cmdlet I use some parameters that are mutually exclusive. if i use...

[Parameter(Position = 2, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "EXE")]
            [ValidateNotNullOrEmpty()]
            public string Arguments { get; set; }

the other parameter

[Parameter(Position = 4, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "EXE")]
        public object[] ModifyUninstallString { get; set; }

should no longer be usable for the user. and reversed.i tryied ParameterSetName but without success. with dynamic parameters I can only initialize both parameters once. A change to the other parameter does not work. :-(

my attempt:

namespace myNamespace
{
    public class NormalArgument
    {
        [Parameter(Position = 2, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "EXE")]
        [ValidateNotNullOrEmpty()]
        public string Arguments { get; set; }
    }

    public class ModifyArgument
    {
        [Parameter(Position = 4, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "EXE")]
        public object[] ModifyUninstallString { get; set; }
    }


    [Cmdlet(VerbsLifecycle.Uninstall, "Legacy")]
    [OutputType(typeof(UninstallLegacy))]
    public partial class UninstallLegacy : PSCmdlet, IDynamicParameters
    {
        private ModifyArgument modArgument = null;
        private NormalArgument normArgument = null;

        public object GetDynamicParameters()
        {
            if (modArgument == null)
            {
                modArgument = new ModifyArgument();
                return modArgument;
            }
            if (normArgument == null)
            {
                normArgument = new NormalArgument();
                return modArgument;
            }
            if (modArgument != null)
            {
                normArgument = null;
            }
            if (normArgument != null)
            {
                modArgument = null;
            }
            return null;
        }

        protected override void ProcessRecord()
        {

        }
    }
}

how can i do this?

thanks for helping!

Chris

c# binary cmdlet
1个回答
0
投票

Found the Solution! thought too complicated!

only the mutually exclusive parameters should have different ParameterSetName. all other not.

Thank you for the time you have already invested!

Chris

© www.soinside.com 2019 - 2024. All rights reserved.