Why is a plus operator required in some Powershell type names? Why is a plus operator required in some Powershell type names? powershell powershell

Why is a plus operator required in some Powershell type names?


System.Environment.SpecialFolder is definitely a type

Type SpecialFolder, which is nested inside type Environment, is located in namespace System:

  • C# references that type as a full type name as in the quoted passage; that is, it uses . not only to separate the namespace from the containing type's name, but also to separate the latter from its nested type's name.

  • By contrast, PowerShell uses a .NET reflection method, Type.GetType(), to obtain a reference to the type at runtime:

    • That method uses a language-agnostic notation to identify types, as specified in documentation topic Specifying fully qualified type names.Tip of the hat to PetSerAl.

    • In that notation, it is + that is used to separate a nested type from its containing type (not ., as in C#).

That is, a PowerShell type literal ([...]) such as:

[System.Environment+SpecialFolder]

is effectively the same as taking the content between [ and ], System.Environment+SpecialFolder, and passing it as a string argument to Type.GetType, namely (expressed in PowerShell syntax):

[Type]::GetType('System.Environment+SpecialFolder')

Note that PowerShell offers convenient extensions (simplifications) to .NET's language-agnostic type notation, notably the ability to use PowerShell's type accelerators (such as [regex] for [System.Text.RegularExpressions.Regex]), the ability to omit the System. prefix from namespaces (e.g. [Collections.Generic.List`1[string]] instead of [System.Collections.Generic.List`1[string]]), and not having to specify the generic arity (e.g. `1) when a list of type argument is passed (e.g. [Collections.Generic.List[string]] instead of [Collections.Generic.List`1[string]] - see this answer) for more information.