How to initialize an array of custom objects

后端 未结 9 2477
粉色の甜心
粉色の甜心 2020-12-13 23:44

First, as this leads to my question, I\'ll start by noting that I\'ve worked with XML a fair bit in PowerShell, and like how I can read data from XML files, quickly, into ar

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 00:30

    A little variation on classes. Initialize it with hashtables.

    class Point { $x; $y }
    
    $a = [Point[]] (@{ x=1; y=2 },@{ x=3; y=4 })
    
    $a
    
    x y        
    - -          
    1 2
    3 4
    
    $a.gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Point[]                                  System.Array
    
    $a[0].gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    Point                                    System.Object
    

提交回复
热议问题