是否可以使用变量来引用PowerShell中的.NET静态类?

Thr*_*aka 1 powershell

我正在做很多System.IO.Path操作,我很好奇是否有可能在变量中存储对该静态类的引用,所以它更短?

而不是编写这些长卷曲的namespace.class路径:

[System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($targetFile), [System.IO.Path]::GetFileName("newfile_$targetFile"))
Run Code Online (Sandbox Code Playgroud)

写这个会很棒:

$path = [System.IO.Path]
$path.Combine($path.GetDirectoryName($targetFile), $path.GetFileName("newfile_$targetFile"))
Run Code Online (Sandbox Code Playgroud)

有没有办法在powershell中做到这一点?

lat*_*kin 5

是.您建议的代码很接近,您只需要使用::静态调用语法:

$path = [System.IO.Path]
$path::Combine( ... )
Run Code Online (Sandbox Code Playgroud)