How to initialize an array of custom objects

后端 未结 9 2440
粉色の甜心
粉色の甜心 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:31

    I had to create an array of a predefined type, and I successfully did as follows:

    [System.Data.DataColumn[]]$myitems = ([System.Data.DataColumn]("col1"), 
                    [System.Data.DataColumn]("col2"),  [System.Data.DataColumn]("col3"))
    
    0 讨论(0)
  • 2020-12-14 00:34

    Use a "Here-String" and cast to XML.

    [xml]$myxml = @"
    <stuff>
     <item name="Joe" age="32">
      <info>something about him</info>
     </item>
     <item name="Sue" age="29">
      <info>something about her</info>
     </item>
     <item name="Cat" age="12">
      <info>something else</info>
     </item>
    </stuff>
    "@
    
    [array]$myitems = $myxml.stuff.Item
    
    $myitems
    
    0 讨论(0)
  • 2020-12-14 00:36

    Here is a concise way to initialize an array of custom objects in PowerShell.

    > $body = @( @{ Prop1="1"; Prop2="2"; Prop3="3" }, @{ Prop1="1"; Prop2="2"; Prop3="3" } )
    > $body
    
    Name                           Value
    ----                           -----
    Prop2                          2
    Prop1                          1
    Prop3                          3
    Prop2                          2
    Prop1                          1
    Prop3                          3  
    
    0 讨论(0)
提交回复
热议问题