为什么在某些Powershell类型名称中需要加号运算符? [英] Why is a plus operator required in some Powershell type names?

查看:137
本文介绍了为什么在某些Powershell类型名称中需要加号运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在Powershell中, System.DayOfWeek 枚举可以像 [System.DayOfWeek] ,而 System.Environment.SpecialFolder 枚举必须像 [System.Environment + SpecialFolder] 一样引用(请注意加号)?

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

我的猜测是因为SpecialFolder是静态Environment类的一部分,而DayOfWeek直接位于System命名空间中,但是我很难找到关于此的任何信息。通常静态成员将使用静态成员运算符,但是在这种情况下不起作用,除了神秘的加号之外,我也没有尝试其他操作...

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绝对是一种类型,在C#中,两个枚举的工作方式相同:

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

我真的很想知道为什么在Powershell中的区别以及此行为背后的原因。有人知道为什么会这样吗?

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?

推荐答案


System.Environment .SpecialFolder 绝对是一种类型

Type SpecialFolder ,它位于 内部类型环境 ,位于命名空间 系统


  • C#引用的引用为完整类型名称,如引文中所述;也就是说,它使用不仅将命名空间与包含类型的名称分离,而且还将将后者与嵌套类型的名称分离。

  • 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.

相比之下, PowerShell使用.NET reflection 方法 Type.GetType() ,以便在运行时获取对该类型的引用:

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.

在这种表示法中,它是 + at用于将嵌套类型与其包含的类型(不是,如C#)分开。

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

也就是说,PowerShell 类型文字 [...] ),例如:

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

[System.Environment+SpecialFolder]

实际上与获取 [之间的内容相同] System.Environment + SpecialFolder ,并将其作为 string参数传递给 Type.GetType ,即(用PowerShell语法表示):

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

这篇关于为什么在某些Powershell类型名称中需要加号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆