仅当提供其他参数时,我才需要应用条件开关参数。任何人都可以让我知道如何在C#中实现这一目标吗?
我想通过以下方式在Powershell终端上访问命令。
修改
Get-Parameter # this will process other path of the code and won't throw an error. (e.g. Not providing name of parameter, it would return all parameters in the container )
Get-Parameter -Name "Epics" -Writeable # writeable is switch parameter
Get-Parameter -Writeable # should throw an error ( Writeable switch only allows when Name parameter is provided.
以下是C#Commandlet代码。
[Cmdlet(VerbsCommon.Get, "Parameter")]
public class GetParameter : PSCmdlet
{
[Parameter(
Position = 1,
Mandatory = false,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ParameterSetName = "Name")
]
public string Name { get; set; }
[Parameter(Mandatory = true, ParameterSetName = "Name" ,ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public SwitchParameter Writeable { get; set; } = false;
}
答案 0 :(得分:0)
根据评论,我认为您需要这样的内容:
[Cmdlet(VerbsCommon.Get, "Parameter")]
public class GetParameter : PSCmdlet
{
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = "Name")
]
public string Name { get; set; }
[Parameter(
Mandatory = false,
ParameterSetName = "Name",
ValueFromPipeline = true,
Position = 1,
ValueFromPipelineByPropertyName = true)]
public SwitchParameter Writeable { get; set; } = false;
}