如何在 PowerShell 中创建内部类?

2020-10-16 11:10:14 +08:00
 Accessing

如何在 PowerShell 中创建内部类?

PowerShell 版本是 7.0.3.

搜索了很多资料都没有发现解决方案,但我自己倒有个变通的方案,这些内容在后面会有详细介绍.

到底该怎么样在 PowerShell 中创建静态内部类?是不是目前 PowerShell 还没有这种机制?

搜索不到相关内容

我想在 PowerShell 中创建静态内部类,尝试在谷歌上搜索"PowerShell" "inner class",阅读about_Classes - PowerShell | Microsoft Docs,都没有发现有关的结果.

使用 Java 语法无效

在 Java 上的语法是这样的

public class InnerClassTest {
    
    static class inner{
        
        
    }
}

但是在 PowerShell 中这样是不行的


class myPath {
    static class ps {
        static $log = "LOG"
    }
}

输出的结果是

ParserError: inner-class-0.ps1:3:17
Line |
   3 |      static class ps {
     |                  ~
     | Missing '(' in class method parameter list.

我目前的方式

这样的方式可以达到类似的效果,不过 VSCode 没有办法对内部类中的成员提供智能提示,令人感到很遗憾.好在 Tab 可以检测到里面的成员.


class myPathPs{

    static [string]$log = 'LOG'
}

class myPath {
    static [Object]$ps = [myPathPs]
}

[myPath]::ps::log

238 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/715535

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX