Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda