V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Accessing
V2EX  ›  问与答

如何在 PowerShell 中创建内部类?

  •  
  •   Accessing · Oct 16, 2020 · 503 views
    This topic created in 2022 days ago, the information mentioned may be changed or developed.

    如何在 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
    
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2736 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:26 · PVG 22:26 · LAX 07:26 · JFK 10:26
    ♥ Do have faith in what you're doing.