Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

359
Visualizações
Conjuntos de parámetros mutuamente excluyentes en C# Powershell Cmdlets: ¿Por qué es ambiguo?

Así que originalmente pedí esto para un cmdlet escrito en PowerShell, consulte: Pregunta ambigua en PowerShell

La respuesta a esa pregunta funciona, pero solo para cmdlets de secuencias de comandos de PowerShell. Así que aquí está de nuevo la pregunta, pero para los cmdlets de C#...

He estado tratando de hacer que funcionen varios conjuntos de exclusiones mutuas. Quiero que "Ancho" se excluya mutuamente con "WidthReset" y "Height" se excluya mutuamente con "HeightReset".

La ayuda para el cmdlet de C# muestra:

 Get-ArgTest [-Width <Object>] [-Height <Object>] [<CommonParameters>] Get-ArgTest [-Width <Object>] [-HeightReset] [<CommonParameters>] Get-ArgTest [-Height <Object>] [-WidthReset] [<CommonParameters>] Get-ArgTest [-WidthReset] [-HeightReset] [<CommonParameters>]

La ayuda para el cmdlet PS muestra:

 PS C:\> .\get-ArgTestPs.ps1 -? get-ArgTestPs.ps1 [-Width <int>] [-Height <int>] [<CommonParameters>] get-ArgTestPs.ps1 [-Width <int>] [-HeightReset] [<CommonParameters>] get-ArgTestPs.ps1 [-Height <int>] [-WidthReset] [<CommonParameters>] get-ArgTestPs.ps1 -WidthReset -HeightReset [<CommonParameters>]

Consulte el siguiente código.

Ahora Get-ArgTest -Width 5 parece ser ambiguo.

¿Por qué es diferente a los cmdlets implementados directamente en PowerShell?

Salida de ejemplo:

 PS C:\> Get-ArgTest -Width 5 -HeightReset Begin! HELLO, Width=5, Height=, WidthReset=False, HeightReset=True PS C:\> Get-ArgTest -Width 5 Get-ArgTest: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided. PS

Los exámenes:

 ## Try each combination # First, just the values Get-ArgTest # FAIL Get-ArgTest -Width 2 # FAIL Get-ArgTest -Height 4 # FAIL Get-ArgTest -Width 2 -Height 4 # Values and the opposite reset Get-ArgTest -Width 2 -HeightReset Get-ArgTest -Height 4 -WidthReset # The Resets Get-ArgTest -WidthReset -HeightReset Get-ArgTest -WidthReset -HeightReset:$False Get-ArgTest -WidthReset:$False -HeightReset:$True Get-ArgTest -WidthReset Get-ArgTest -HeightReset

El código C# es:

 [Cmdlet("Get", "ArgTest")] [CmdletBinding(DefaultParameterSetName = "A")] public class ArgTestCmdlet : PSCmdlet { [Parameter(ParameterSetName = "A")] [Parameter(ParameterSetName = "B")] public object Width; [Parameter(ParameterSetName = "C")] [Parameter(ParameterSetName = "A")] public object Height; [Parameter(ParameterSetName = "C")] [Parameter(ParameterSetName = "D", Mandatory = true)] public SwitchParameter WidthReset; [Parameter(ParameterSetName = "B")] [Parameter(ParameterSetName = "D", Mandatory = true)] public SwitchParameter HeightReset; // This method gets called once for each cmdlet in the pipeline when the pipeline starts executing protected override void BeginProcessing() { WriteObject("Begin!"); } // This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called protected override void ProcessRecord() { } // This method will be called once at the end of pipeline execution; if no input is received, this method is not called protected override void EndProcessing() { WriteObject($"HELLO, Width={Width}, Height={Height}, WidthReset={WidthReset}, HeightReset={HeightReset}"); } }
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

El atributo CmdletBinding es para declarar un comportamiento de enlace similar a un cmdlet en cmdlets de secuencias de comandos (o "funciones avanzadas", como a veces se les llama).

Para un cmdlet binario, debe especificar el nombre del conjunto de parámetros predeterminado en el decorador de Cmdlet :

 [Cmdlet("Get", "ArgTest", DefaultParameterSetName = "A")] public class ArgTestCmdlet : PSCmdlet { // the rest stays the same ... }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda