Home > Mobile >  F# - Property 'ParameterSetName' is not static
F# - Property 'ParameterSetName' is not static

Time:11-21

I write a PowerShell function in F# with four parameters.

[<Parameter(
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
[<ValidateNotNullOrEmpty>]
member val Payload : Hashtable = Hashtable () with get, set
[<Parameter(
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
member val Type : string = String.Empty with get, set
[<Parameter(
  ParameterSetName="Key",
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
[<ValidateNotNullOrEmpty>]
member val Secret : string = String.Empty with get, set
[<Parameter(
  ParameterSetName="FilePath",
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
[<ValidateNotNullOrEmpty>]
member val FilePath : System.IO.FileInfo = null with get, set

Ideally, I would like to use match for the ParameterSetNames but I get the error/warning that this property is no static. Property 'ParameterSetName' is not static

To clarify what I mean by using match. I've got this code

let result = 
  match PSCmdlet.ParameterSetName with
    | "Key" -> newSecretWithContent x.Algorithm x.Payload x.Secret
    | "FilePath" -> newJwtWithFile x.Algorithm x.Payload x.FilePath.FullName
    | _ -> "Incorrect ParameterSet selected."

and the compiler gives me this warning:

Property 'ParameterSetName' is not static F# Compiler (806)

CodePudding user response:

I have never written a PowerShell cmdlet, but it seems that PSCmdlet.ParameterSetName refers to an instance member of the PSCmdlet class (see MSDN). In PowerShell, you seem to be able to write $PSCmdlet.ParameterSetName because $PSCmdlet refers to the current cmdlet.

In F#, the current cmdlet is your "this" instance, i.e. x. According to the documentation, you can write cmdlets either by inheriting from Cmdlet or by deriving from PSCmdlet which gives you access to more of the runtime. If you dervie from PSCmdlet, you should be able to refer to this via x and write:

let result = 
  match x.ParameterSetName with
    | "Key" -> newSecretWithContent x.Algorithm x.Payload x.Secret
    | "FilePath" -> newJwtWithFile x.Algorithm x.Payload x.FilePath.FullName
    | _ -> "Incorrect ParameterSet selected."
  • Related