在 powershell 中输入完全限定的类型名称?
Typing fully qualified type names in powershell?
我想减少键入 System
和其他根命名空间,例如
[string] $ComputerName = [Environment]::MachineName
对比
[System.String] $ComputerName = [System.Environment]::MachineName
另一个例子:
[Net.Dns]::GetHostEntry("192.168.1.1")
对比
[System.Net.Dns]::GetHostEntry("192.168.1.1")
键入 System
和需要类似的父名称空间时,是否有任何原因和具体情况?
我经常想知道为什么还有 System
命名空间,因为一切都在该命名空间内,那么如何处理该命名空间呢?这是胡说八道; System
这个词到底是什么意思?这里与操作系统无关。但对于 NET 框架中的所有内容。
我假设调用静态方法时可能会出现异常,但我不会C#所以无法自己回答。
顺序有点乱:
it's a nonsense; what is the term System
supposed to mean anyway?
有问题的 "System" 是 .NET 运行时和框架。据我所知,最初的想法是 with .NET 不附带的代码将属于不同的命名空间 - 但构建在 .NET 之上的多个 Microsoft 编写的组件有因为使用了 System
父命名空间 - 包括构成 PowerShell 本身的所有 API。
I often wonder why is there [a] System
namespace after all since everything is inside that namespace so what the deal with that namespace?
"everything" 不在 System
命名空间内,但如上所述,运行时或基础 class 库随附的所有内容都在 - 这正是 PowerShell 自动解析类型的原因即使您从合格的类型名称中省略 System.
也仍然是文字 - PowerShell 已经在尝试帮助您减少输入
Are there any reasons and specific situation when typing System and similar parent namespaces is required?
是 - 当父命名空间 不是 System
.
第一个想到的例子是 Windows 上 Win32 注册表 API 的 .NET 包装器 classes:
$HKLM = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Default)
现在,对于实际问题:
I want to reduce typing System
and other root namespaces
您不能将自定义命名空间前缀(如 System
)添加到 PowerShell 的名称解析方法,但您可以声明类型名称的自动解析 PowerShell 5 及更高版本中的特定命名空间,带有 using namespace
directive.
没有using namespace
:
[System.Net.Dns]::GetHostEntry("192.168.1.1")
与using namespace
:
using namespace System.Net
[Dns]::GetHostEntry("192.168.1.1")
在脚本中使用时,任何 using
指令必须位于文件中的任何其他指令之前,包括 param
块。
using namespace
指令也将在交互式会话中工作,前提是您将其作为单独的语句发出:
PS> using namespace System.Net
PS> [Dns] # still works!
对了,powershell有很多"type accelerators"像[datetime],你能不能自己做一个,像[dt]:
我想减少键入 System
和其他根命名空间,例如
[string] $ComputerName = [Environment]::MachineName
对比
[System.String] $ComputerName = [System.Environment]::MachineName
另一个例子:
[Net.Dns]::GetHostEntry("192.168.1.1")
对比
[System.Net.Dns]::GetHostEntry("192.168.1.1")
键入 System
和需要类似的父名称空间时,是否有任何原因和具体情况?
我经常想知道为什么还有 System
命名空间,因为一切都在该命名空间内,那么如何处理该命名空间呢?这是胡说八道; System
这个词到底是什么意思?这里与操作系统无关。但对于 NET 框架中的所有内容。
我假设调用静态方法时可能会出现异常,但我不会C#所以无法自己回答。
顺序有点乱:
it's a nonsense; what is the term
System
supposed to mean anyway?
有问题的 "System" 是 .NET 运行时和框架。据我所知,最初的想法是 with .NET 不附带的代码将属于不同的命名空间 - 但构建在 .NET 之上的多个 Microsoft 编写的组件有因为使用了 System
父命名空间 - 包括构成 PowerShell 本身的所有 API。
I often wonder why is there [a]
System
namespace after all since everything is inside that namespace so what the deal with that namespace?
"everything" 不在 System
命名空间内,但如上所述,运行时或基础 class 库随附的所有内容都在 - 这正是 PowerShell 自动解析类型的原因即使您从合格的类型名称中省略 System.
也仍然是文字 - PowerShell 已经在尝试帮助您减少输入
Are there any reasons and specific situation when typing System and similar parent namespaces is required?
是 - 当父命名空间 不是 System
.
第一个想到的例子是 Windows 上 Win32 注册表 API 的 .NET 包装器 classes:
$HKLM = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Default)
现在,对于实际问题:
I want to reduce typing
System
and other root namespaces
您不能将自定义命名空间前缀(如 System
)添加到 PowerShell 的名称解析方法,但您可以声明类型名称的自动解析 PowerShell 5 及更高版本中的特定命名空间,带有 using namespace
directive.
没有using namespace
:
[System.Net.Dns]::GetHostEntry("192.168.1.1")
与using namespace
:
using namespace System.Net
[Dns]::GetHostEntry("192.168.1.1")
在脚本中使用时,任何 using
指令必须位于文件中的任何其他指令之前,包括 param
块。
using namespace
指令也将在交互式会话中工作,前提是您将其作为单独的语句发出:
PS> using namespace System.Net
PS> [Dns] # still works!
对了,powershell有很多"type accelerators"像[datetime],你能不能自己做一个,像[dt]: