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

隐身守侯 提交于 2020-04-10 08:38:11

问题


Why is it that, in Powershell, the System.DayOfWeek enum can be referred to like [System.DayOfWeek], whereas the System.Environment.SpecialFolder enum must be referred to like [System.Environment+SpecialFolder] (note the plus character)?

My guess is because SpecialFolder is part of the static Environment class and DayOfWeek is sitting directly in the System namespace, but I'm having trouble finding any information on this. Normally static members would use the "static member operator", but that doesn't work in this case, nor does anything else I try except the mysterious plus character...

[System.DayOfWeek] # returns enum type
[enum]::GetValues([System.DayOfWeek]) # returns enum values
[enum]::GetValues([System.Environment.SpecialFolder]) # exception: unable to find type
[enum]::GetValues([System.Environment]::SpecialFolder) # exception: value cannot be null
[enum]::GetValues([System.Environment+SpecialFolder]) # returns enum values

System.Environment.SpecialFolder is definitely a type, and in C# both enums work the same way:

Enum.GetValues(typeof(System.Environment.SpecialFolder)) // works fine
Enum.GetValues(typeof(System.DayOfWeek)) // also works

I'd really like to understand why there's a distinction in Powershell and the reasoning behind this behaviour. Does anyone know why this is the case?


回答1:


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')


来源:https://stackoverflow.com/questions/57228424/why-is-a-plus-operator-required-in-some-powershell-type-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!