What is [cmdletbinding()] and how does it work?

后端 未结 3 1097
慢半拍i
慢半拍i 2020-12-11 00:14

According to get-help about_Functions_CmdletBindingAttribute

The CmdletBinding attribute is an attribute of functions that makes them operate like co

3条回答
  •  失恋的感觉
    2020-12-11 00:57

    Regarding the syntax question, the format closely matches how you apply a .NET attribute class to a member using named parameters in C#.

    Compare the (simplified) grammar for attributes from section B.2.4 of The PowerShell Language Specification with that from section C.2.13 of the C# Language Specification:

    B.2.4 Attributes   (PowerShell)

    attribute:
      [ attribute-name ( attribute-arguments ) ]

    attribute-arguments:
      attribute-argument
      attribute-argument
    , attribute-arguments

    attribute-argument:
      simple-name
    = expression


    C.2.13 Attributes   (C#)

    attribute:
      [ attribute-name ( named-argument-list ) ]

    named-argument-list:
      named-argument
      named-argument-list
    , named-argument

    named-argument:
      identifier
    = attribute-argument-expression



    I agree it might have been nice from a sense of conceptual brevity to e.g. re-use hashtable initialization syntax for attribute initialization. However, I can imagine supporting all the options from hashtables (like [A(P=v)] and [A('P'=v)] and $n = 'P'; [A($n=v)] and such, or some particular subset of them) just to use ; as the separator character would have been more trouble than it was worth.

    On the other hand, if you want to use advanced functions, then maybe it makes sense to learn an advanced syntax :)

提交回复
热议问题